blob: 67b292ff68a79cd2be973e2a7037a47c5f7732c8 [file] [log] [blame]
Sylvain Munaut26bc4652020-09-14 10:19:49 +02001/*
2 * utils.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
5 * SPDX-License-Identifier: LGPL-3.0-or-later
6 */
7
8#include <stdint.h>
9#include <stdbool.h>
10
11char *
12hexstr(void *d, int n, bool space)
13{
14 static const char * const hex = "0123456789abcdef";
15 static char buf[96];
16 uint8_t *p = d;
17 char *s = buf;
18 char c;
19
20 while (n--) {
21 c = *p++;
22 *s++ = hex[c >> 4];
23 *s++ = hex[c & 0xf];
24 if (space)
25 *s++ = ' ';
26 }
27
28 s[space?-1:0] = '\0';
29
30 return buf;
31}