Created
April 21, 2009 20:56
-
-
Save kulp/99379 to your computer and use it in GitHub Desktop.
Convert a hex string into integer or character buffer
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
static inline char _hexify(const char c) | |
{ | |
return (((c | ('A' ^ 'a')) - '0') % 39); | |
} | |
/// Converts a len-digit hex number into its unsigned native representation. | |
unsigned int dehex_to_int(int ilen, const char ibuf[ilen]) | |
{ | |
unsigned int result = 0; | |
for (int i = 0; i < ilen; i++) | |
result |= _hexify(ibuf[ilen - i - 1]) << (i << 2); | |
return result; | |
} | |
void dehex_to_buf(int ilen, const char ibuf[ilen], int olen, char obuf[olen]) | |
{ | |
for (int i = 0; i < ilen && i >> 1 < olen; i++) | |
obuf[i >> 1] |= _hexify(ibuf[ilen - i - 1]) << ((i & 1) << 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment