-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.3.c
136 lines (123 loc) · 3.08 KB
/
2.3.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAXLINE 1024
int hex_validator(unsigned const char *, int);
long long int htoi(unsigned const char *);
long long int my_exp(int, int);
int
hex_validator(unsigned const char *s, int len)
{
int retval = 0;
int i = 0;
/* process minus sign */
if (s[i] == '-') {
i++;
}
/* process 0x or 0X hex identifier */
if (s[i] == '0') {
i++;
if ((s[i] != 'x') && (s[i] != 'X')) {
goto out;
} else {
i++;
}
}
while (i < len - 1) {
if (
(((s[i] >= '0') && (s[i] <= '9')) ||
((s[i] >= 'A') && (s[i] <= 'F')) ||
((s[i] >= 'a') && (s[i] <= 'f'))))
{
i++;
} else {
break;
}
}
if (i == len - 1) retval = 1;
out:
return retval;
}
long long int
my_exp(int base, int exp)
{
long long int value = 1;
while (exp != 0) {
value *= base; /* value = value*base; */
--exp;
}
return value;
}
/*
* http://stackoverflow.com/questions/10324/how-can-i-convert-a-hexadecimal-number-to-base-10-efficiently-in-c
* @brief convert a hexidecimal string to a signed long
* will not produce or process negative numbers except
* to signal error.
*
* @param hex without decoration, case insensative.
*
* @return -1 on error, or result (max sizeof(long)-1 bits)
*/
/*
static const long gnu_hextable[] = {
[0 ... 255] = -1,
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
['A'] = 10, 11, 12, 13, 14, 15,
['a'] = 10, 11, 12, 13, 14, 15
};
*/
static const long hextable[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1
};
long long int htoi(unsigned const char *hex) {
long long ret = 0;
while (*hex && ret >= 0) {
ret = (ret << 4) | hextable[*hex++];
}
return ret;
}
int
main()
{
int exit_status = 0;
int i = 0;
unsigned char s[MAXLINE];
int c;
while (i < MAXLINE - 1) {
c = getchar();
if (c == '\n') break;
if (c == '\0') break;
if (c == EOF) break;
s[i++] = c;
}
s[i++] = '\0';
/*
if (! hex_validator(s, i)) {
fprintf(stderr, "not valid hex!\n");
exit_status = 1;
goto out;
}
*/
long long int result = htoi(s);
if (result < 0) {
exit_status = 1;
goto out;
}
printf("%lld\n", result);
out:
return exit_status;
}