/
numerate_number.c
159 lines (136 loc) · 2.58 KB
/
numerate_number.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdlib.h>
// void* calloc(int count, int size);
#define TRUE 1
//CONSTANT TRUE 1
#define FALSE 0
//CONSTANT FALSE 0
int in_set(int c, char* s);
//CONSTANT NULL 0
char* numerate_number(int a)
{
char* result = calloc(16, sizeof(char));
if(NULL == result) return NULL;
int i = 0;
/* Deal with Zero case */
if(0 == a)
{
result[0] = '0';
return result;
}
/* Deal with negatives */
if(0 > a)
{
result[0] = '-';
i = 1;
a = a * -1;
}
/* Using the largest 10^n number possible in 32bits */
int divisor = 0x3B9ACA00;
/* Skip leading Zeros */
while(0 == (a / divisor)) divisor = divisor / 10;
/* Now simply collect numbers until divisor is gone */
while(0 < divisor)
{
result[i] = ((a / divisor) + 48);
a = a % divisor;
divisor = divisor / 10;
i = i + 1;
}
return result;
}
int char2hex(int c)
{
if (c >= '0' && c <= '9') return (c - 48);
else if (c >= 'a' && c <= 'f') return (c - 87);
else if (c >= 'A' && c <= 'F') return (c - 55);
else return -1;
}
int hex2char(int c)
{
if((c >= 0) && (c <= 9)) return (c + 48);
else if((c >= 10) && (c <= 15)) return (c + 55);
else return -1;
}
int char2dec(int c)
{
if (c >= '0' && c <= '9') return (c - 48);
else return -1;
}
int dec2char(int c)
{
if((c >= 0) && (c <= 9)) return (c + 48);
else return -1;
}
int index_number(char* s, char c)
{
int i = 0;
while(s[i] != c)
{
i = i + 1;
if(0 == s[i]) return -1;
}
return i;
}
int toupper(int c)
{
if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF);
return c;
}
int set_reader(char* set, int mult, char* input)
{
int n = 0;
int i = 0;
int hold;
int negative_p = FALSE;
if(input[0] == '-')
{
negative_p = TRUE;
i = i + 1;
}
while(in_set(input[i], set))
{
n = n * mult;
hold = index_number(set, toupper(input[i]));
/* Input managed to change between in_set and index_number */
if(-1 == hold) return 0;
n = n + hold;
i = i + 1;
}
/* loop exited before NULL and thus invalid input */
if(0 != input[i]) return 0;
if(negative_p)
{
n = 0 - n;
}
return n;
}
int numerate_string(char *a)
{
/* If NULL string */
if(0 == a[0]) return 0;
/* Deal with binary*/
else if ('0' == a[0] && 'b' == a[1])
{
return set_reader("01", 2, a+2);
}
/* Deal with hex */
else if ('0' == a[0] && 'x' == a[1])
{
return set_reader("0123456789ABCDEFabcdef", 16, a+2);
}
/* Deal with octal */
else if('0' == a[0])
{
return set_reader("01234567", 8, a+1);
}
/* Deal with decimal */
else
{
return set_reader("0123456789", 10, a);
}
}