-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathconvert.c
114 lines (88 loc) · 1.56 KB
/
convert.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
/*
Several examples of conversion from one form to another.
*/
#include <stdio.h>
int atoi(char s[]);
int htoi(const char s[]);
int hex2int(int h);
/* convert character to an integer */
int atoi(char s[])
{
int i, n;
n = 0;
for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n * (s[i] - '0');
return n;
}
/* covert c to lower case { ASCII Only } */
int lower(int c)
{
if(c >= 'A' && c <= 'Z')
return c + 'a' - 'A';
else
return c;
}
/* converts string to hex digits */
/*
Converts string of hexadecimal digits (including optonal 0x or 0X into its equivalent integer value.
The allowable digits are 0-9, a-f, A-F.
*/
int htoi(const char s[])
{
int i = 0;
int ans = 0;
int valid = 1;
int hexit;
// skip over 0x or 0X
if(s[i] == '0')
{
++i;
if(s[i] == 'x' || s[i] == 'X'){ ++i; }
}
while(valid && s[i] != '\0')
{
ans = ans * 16;
if(s[i] >= '0' && s[i] <= '9')
{
ans = ans + (s[i] - '0');
} else {
hexit = hex2int(s[i]);
if(hexit == 0){ valid = 0; } else { ans = ans + hexit; }
}
++i;
}
if(!valid) { ans = 0; }
return ans;
}
/* convert hex chars to integers return integer value */
int hex2int(int h)
{
char options[] = { "AaBbCcDdEeFf" };
int val = 0;
int i;
for(i = 0; val == 0 && options[i] != '\0'; i++)
{
if(h == options[i]) { val = 10 + (i/2); }
}
return val;
}
int main()
{
char *test[] = // declare test as array of pointer to char
{
"0xf01",
"0xA",
"a",
"0xB",
"23",
"100"
};
int res = 0;
int i = 0;
for(i = 0; i < 6; i++)
{
res = htoi(test[i]);
printf("%d\n", res);
}
return 0;
}