Skip to content

Instantly share code, notes, and snippets.

@Tomaszal
Created February 25, 2018 13:01
Show Gist options
  • Save Tomaszal/e5f2035655d1f2e4220e0a8199e406ef to your computer and use it in GitHub Desktop.
Save Tomaszal/e5f2035655d1f2e4220e0a8199e406ef to your computer and use it in GitHub Desktop.
Hexadecimal C string to decimal integer converter function.
#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