Skip to content

Instantly share code, notes, and snippets.

@hyperreality
Created September 11, 2014 20:06
Show Gist options
  • Select an option

  • Save hyperreality/978d565d8075f3527ad7 to your computer and use it in GitHub Desktop.

Select an option

Save hyperreality/978d565d8075f3527ad7 to your computer and use it in GitHub Desktop.
Hex to Int (Kernighan & Ritchie 2-3)
#include <stdio.h>
#include <ctype.h>
int htio(char s[])
{
int i,n;
n=0;
for (i=0; isxdigit(s[i]); ++i)
{
n *= 16;
if(isdigit(s[i]))
n += s[i] - '0';
else
n += toupper(s[i]) - 'A' + 0xA;
}
return n;
};
int main(void) {
char *s;
s = "12ab";
printf("%d", htio(s));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment