Last active
December 31, 2015 02:49
-
-
Save nitrix/7923399 to your computer and use it in GitHub Desktop.
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 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