Skip to content

Instantly share code, notes, and snippets.

@nitrix
Last active December 31, 2015 02:49
Show Gist options
  • Select an option

  • Save nitrix/7923399 to your computer and use it in GitHub Desktop.

Select an option

Save nitrix/7923399 to your computer and use it in GitHub Desktop.
static const long hextable[] = {
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
['A'] = 10, 11, 12, 13, 14, 15,
['a'] = 10, 11, 12, 13, 14, 15
};
/*
* Convert a hex string to its decimal number.
* Unlimited length.
* Won't produce negative values.
* Works with odd lengths. (0xfff)
* Should return -1 on error.
*/
long hexdec(const char *hex) {
long ret = 0;
while (*hex && ret >= 0) {
ret = (ret << 4) | hextable[*hex++];
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment