Created
September 11, 2014 20:06
-
-
Save hyperreality/978d565d8075f3527ad7 to your computer and use it in GitHub Desktop.
Hex to Int (Kernighan & Ritchie 2-3)
This file contains hidden or 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 <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