-
Notifications
You must be signed in to change notification settings - Fork 0
/
htoi.c
55 lines (43 loc) · 1.09 KB
/
htoi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Hexadecimal to integer. */
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "stdlib.h"
void str2lower(char *dst);
unsigned long long power(unsigned long long base, unsigned long long exp);
int main(void) {
char hex[] = "0xX0xD0x80x60x70x80x80x80x70x90x20xA0x10x40x3";
char h;
unsigned long long len = strlen(hex) / 3;
unsigned long long dec;
size_t i = i;
str2lower(hex);
dec = 0;
for (i = 0; i < len; ++i) {
h = *(hex + (i + 1) * 3 - 1);
if (h >= '0' && h <= '9')
dec += (h - '0') * power(16, len - i - 1);
else if (h >= 'a' && h <= 'f')
dec += (h - 'a' + 10) * power(16, len - i - 1);
else {
perror("invalid hexadecimal");
exit(EXIT_FAILURE);
}
}
printf("%llu\n", dec);
}
/* Convert string to lowercase version. */
void str2lower(char *dst) {
size_t i;
for (i = 0; dst[i] != '\0'; ++i)
dst[i] = tolower(dst[i]);
}
/* Power */
unsigned long long power(unsigned long long base, unsigned long long exp) {
unsigned long long out;
out = 1;
for (; exp > 0; exp--) {
out *= base;
}
return out;
}