logo

utils-std

Collection of commonly available Unix tools
commit: 40eaa1f5c8c1ca6de9773435869dbf844823cd6c
parent 5a66298eea637fe9abf2c56f896ceca8f2c39e03
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 15 Aug 2024 15:06:11 +0200

lib/bytes2hex.c: new

Diffstat:

Alib/bytes2hex.c24++++++++++++++++++++++++
Alib/strconv.h8++++++++
2 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/lib/bytes2hex.c b/lib/bytes2hex.c @@ -0,0 +1,24 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: 0BSD + +#define _POSIX_C_SOURCE 200809L +#include "./strconv.h" + +#include <assert.h> + +void +bytes2hex(const uint8_t *data, size_t datalen, char *buf, size_t buflen) +{ + static char *hextab = "0123456789abcdef"; + + assert(buflen >= datalen * 2); + + size_t bi = 0; + for(size_t i = 0; i < datalen; i++) + { + buf[bi++] = hextab[data[i] / 0x10]; + buf[bi++] = hextab[data[i] % 0x10]; + } + if(bi < buflen) buf[bi] = '\0'; +} diff --git a/lib/strconv.h b/lib/strconv.h @@ -0,0 +1,8 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: 0BSD + +#include <stdint.h> // uint8_t +#include <sys/types.h> // size_t + +void bytes2hex(const uint8_t *data, size_t datalen, char *buf, size_t buflen);