Skip to content

Instantly share code, notes, and snippets.

@kulp
Created April 21, 2009 20:56
Show Gist options
  • Save kulp/99379 to your computer and use it in GitHub Desktop.
Save kulp/99379 to your computer and use it in GitHub Desktop.
Convert a hex string into integer or character buffer
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