Created
November 14, 2019 05:44
-
-
Save shawnfeng0/71483d51e5ff10ae5f6eb28084b3804c to your computer and use it in GitHub Desktop.
Convert hexadecimal to a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char *Hex2Str(uint8_t *addr, size_t len, char *out_str, size_t out_str_len, | |
bool from_end) { | |
if (addr == NULL || len == 0 || out_str == NULL || out_str_len < 3) | |
return out_str; | |
len = len < (out_str_len-1) / 2 ? len : (out_str_len-1) / 2; | |
char hex[] = "0123456789ABCDEF"; | |
char *str = out_str; | |
if (from_end) { | |
for (addr += len; len > 0; len--) { | |
*str++ = hex[*--addr >> 4u]; | |
*str++ = hex[*addr & 0x0Fu]; | |
} | |
} else { | |
for (; len > 0; len--) { | |
*str++ = hex[*addr >> 4u]; | |
*str++ = hex[*addr++ & 0x0Fu]; | |
} | |
} | |
*str = '\0'; | |
return out_str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment