Sylvain Munaut | 26bc465 | 2020-09-14 10:19:49 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | char * |
| 12 | hexstr(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 | } |