hex_utils: Cleaned up and fixed the type confusion that was going on
This commit is contained in:
parent
adff7651b6
commit
bc5cca7a8b
|
@ -26,16 +26,16 @@
|
|||
|
||||
static const char hexdigits[] = "0123456789abcdef";
|
||||
|
||||
char * hexify(char *hex, const void *buf, size_t size)
|
||||
char *hexify(char *hex, const void *buf, const size_t size)
|
||||
{
|
||||
char *tmp = hex;
|
||||
const uint8_t *b = buf;
|
||||
char *dst = hex;
|
||||
const uint8_t *const src = buf;
|
||||
|
||||
while (size--) {
|
||||
*tmp++ = hexdigits[*b >> 4];
|
||||
*tmp++ = hexdigits[*b++ & 0xF];
|
||||
for (size_t idx = 0; idx < size; ++idx) {
|
||||
*dst++ = hexdigits[src[idx] >> 4];
|
||||
*dst++ = hexdigits[src[idx] & 0xF];
|
||||
}
|
||||
*tmp++ = 0;
|
||||
*dst++ = 0;
|
||||
|
||||
return hex;
|
||||
}
|
||||
|
@ -43,20 +43,18 @@ char * hexify(char *hex, const void *buf, size_t size)
|
|||
static uint8_t unhex_digit(char hex)
|
||||
{
|
||||
uint8_t tmp = hex - '0';
|
||||
if(tmp > 9)
|
||||
if (tmp > 9)
|
||||
tmp -= 'A' - '0' - 10;
|
||||
if(tmp > 16)
|
||||
if (tmp > 16)
|
||||
tmp -= 'a' - 'A';
|
||||
return tmp;
|
||||
}
|
||||
|
||||
char * unhexify(void *buf, const char *hex, size_t size)
|
||||
char *unhexify(void *buf, const char *hex, const size_t size)
|
||||
{
|
||||
uint8_t *b = buf;
|
||||
while (size--) {
|
||||
*b = unhex_digit(*hex++) << 4;
|
||||
*b++ |= unhex_digit(*hex++);
|
||||
uint8_t *const dst = buf;
|
||||
for (size_t idx = 0; idx < size; ++idx, hex += 2) {
|
||||
dst[idx] = (unhex_digit(hex[0]) << 4) | unhex_digit(hex[1]);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue