2 5dc21454 2023-07-14 jrmu * 2-3. Write the function htoi(s), which converts a string of hexadecimal
3 5dc21454 2023-07-14 jrmu * digits (including an optional 0x or 0X) into its equivalent integer
4 5dc21454 2023-07-14 jrmu * value. The allowable digits are 0 through 9, a through f, and A through F.
6 5dc21454 2023-07-14 jrmu #include <stdio.h>
8 5dc21454 2023-07-14 jrmu unsigned int htoi(const char s[]);
11 5dc21454 2023-07-14 jrmu printf("0 %u\n", htoi("0x00000000"));
12 5dc21454 2023-07-14 jrmu printf("1 %u\n", htoi("0x00000001"));
13 5dc21454 2023-07-14 jrmu printf("273 %u\n", htoi("0x00000111"));
14 5dc21454 2023-07-14 jrmu printf("2730 %u\n", htoi("0x00000aaA"));
15 5dc21454 2023-07-14 jrmu printf("2872373078 %u\n", htoi("0xab34ef56"));
16 5dc21454 2023-07-14 jrmu printf("4291686144 %u\n", htoi("0XFFcDeF00"));
17 5dc21454 2023-07-14 jrmu printf("4294967295 %u\n", htoi("0XfffFFFff"));
18 5dc21454 2023-07-14 jrmu printf("2687823702 %u\n", htoi("a034eF56"));
19 5dc21454 2023-07-14 jrmu printf("4294967295 %u\n", htoi("fffFFFff"));
22 5dc21454 2023-07-14 jrmu unsigned int htoi(const char s[]) {
24 5dc21454 2023-07-14 jrmu int val = 0;
25 5dc21454 2023-07-14 jrmu if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
28 5dc21454 2023-07-14 jrmu for (; s[i] != '\0'; ++i) {
29 5dc21454 2023-07-14 jrmu if (s[i] >= '0' && s[i] <= '9') {
30 5dc21454 2023-07-14 jrmu val = 16*val + s[i] - '0';
31 5dc21454 2023-07-14 jrmu } else if (s[i] >= 'a' && s[i] <= 'f') {
32 5dc21454 2023-07-14 jrmu val = 16*val + s[i] - 'a' + 10;
33 5dc21454 2023-07-14 jrmu } else if (s[i] >= 'A' && s[i] <= 'F') {
34 5dc21454 2023-07-14 jrmu val = 16*val + s[i] - 'A' + 10;