Skip to content

Instantly share code, notes, and snippets.

@marchrius
Created November 27, 2014 11:28
Show Gist options
  • Save marchrius/1cc00420bcede3721448 to your computer and use it in GitHub Desktop.
Save marchrius/1cc00420bcede3721448 to your computer and use it in GitHub Desktop.
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