Created
August 26, 2019 13:54
-
-
Save gotjon05/0d13a97d9d4ea595f58065bbe5c05b17 to your computer and use it in GitHub Desktop.
Copied 2.3.c for understanding solution
This file contains 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
#define YES 1 | |
#define NO 0 | |
#include <stdio.h> | |
int htoi(char s[]); | |
int main(){ | |
char s[] = {'3', '0xA', 'a'}; | |
htoi(s); | |
printf("%x", s); | |
} | |
int htoi(char s[]) | |
{ | |
int hexdigit, i, index, n; | |
i=0; | |
if(s[i] == '0'){ | |
++i; | |
if(s[i] == 'x' || s[i] == 'X'){ | |
++i; | |
} | |
} | |
n = 0; | |
index = YES; | |
//i will be the last value assigned to it when the loop starts. | |
for(; index == YES; ++i){ | |
if(s[i] >= '0' && s[i] <= '9'){ | |
hexdigit = s[i] - '0'; | |
} else if(s[i] >= 'a' && s[i] <= 'f'){ | |
hexdigit = s[i] - 'a' + 10; | |
} else if(s[i] >= 'A' && s[i] <= 'F'){ | |
hexdigit = s[i] - 'A' + 10; | |
}else{ | |
index = NO; | |
} | |
if(index == YES){ | |
n = 16 * n + hexdigit; | |
} | |
return n; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment