-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
113 lines (100 loc) · 1.63 KB
/
function.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
#include "function.h"
unsigned int
ui2s(unsigned int num, char *buff, unsigned int bsize, unsigned int base,
unsigned int len)
{
unsigned int i , j, k, l;
char chb , che;
if (!base) {
buff[0] = 0;
return 0;
}
j = num, i = 0;
do {
k = j % base;
j /= base;
buff[i++] = ((base == 16) && (k > 9)) ? k + 'A' - 10 : k + '0';
}
while (j > 0 && i < bsize);
for (; i < len; i++) {
buff[i] = '0';
}
l = i;
buff[i] = 0;
if (i > 0) {
chb = buff[0];
che = buff[i - 1];
for (j = 0, i--; j < i; j++, i--) {
chb = buff[j];
che = buff[i];
buff[j] = che;
buff[i] = chb;
}
}
return l;
}
int
isUint(const char *ch)
{
unsigned int i;
if (!sLen(ch))
return 0;
for (i = 0; ch[i]; i++) {
if (ch[i] < '0' || ch[i] > '9') {
return 0;
}
}
return 1;
}
unsigned int
pow2ui(unsigned int base, unsigned int pow)
{
unsigned int i , j;
if (!base)
return 0;
if (!pow)
return 1;
for (i = 1, j = base; i < pow; i++) {
j = j * base;
}
return j;
}
unsigned int
s2ui(const char *ch, unsigned int base)
{
unsigned int i , j, k, l;
j = 0;
k = 0;
i = sLen(ch) - 1;
do {
if (base == 16) {
l =
(((ch[i] >= 'A'
&& ch[i] <= 'F') ? (ch[i] - 'A' + 10) : (ch[i] - '0')));
j += l * pow2ui(base, k++);
} else {
j += (ch[i] - '0') * pow2ui(base, k++);
}
}
while (i-- != 0);
return j;
}
unsigned int
sLen(const char *ch)
{
unsigned int i;
for (i = 0; ch[i]; i++);
return i;
}
unsigned
isUintHex(const char *ch)
{
unsigned int i;
if (!sLen(ch))
return 0;
for (i = 0; ch[i]; i++) {
if (!((ch[i] >= '0' && ch[i] <= '9') || (ch[i] >= 'A' && ch[i] <= 'F')))
return 0;
}
return 1;
}