Skip to content

Instantly share code, notes, and snippets.

@marchrius

marchrius/hex2int.h

Created Nov 27, 2014
Embed
What would you like to do?
Hexadecimal to Decimal (Int)
#include <math.h>
#include <string.h>
int hex2int(const char * s)
{
char charset[] = "0123456789abcdef";
int i = (int)strlen(s), len = i, num = 0, j = 0;
while (i >= 0) {
for (j = 0; j < 16; j++) {
if (charset[j] == s[i]) {
num += j * (int)pow(16, len-i-1);
break;
}
}
i--;
}
return (num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment