Created
February 25, 2018 13:01
-
-
Save Tomaszal/e5f2035655d1f2e4220e0a8199e406ef to your computer and use it in GitHub Desktop.
Hexadecimal C string to decimal integer converter function.
This file contains 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
#include <assert.h> | |
int hexToDec(char *hex) { | |
int dec = 0; | |
do { | |
assert((*hex >= '0' && *hex <= '9') || (*hex >= 'A' && *hex <= 'F')); | |
dec = (dec << 4) | ((*hex - ((*hex >= 'A') ? ('A' - 10) : '0')) & 0xF); | |
} while (*++hex); | |
return dec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment