-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.c
266 lines (184 loc) · 4.65 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* =====[ xxx.c ]=========================================================
Description:
Compiled: MS-VC.
Compiler opt: See makefile.
Revisions:
Revisions:
REV DATE BY DESCRIPTION
---- -------- ---------- --------------------------------------
0.00 mm/dd/95 Peter Glen Initial version.
======================================================================= */
/* -------- System includes: -------------------------------------------- */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
/* -------- Includes: --------------------------------------------------- */
#include "convert.h"
/* -------- Defines: ----------------------------------------------------- */
/* -------- Macros: ------------------------------------------------------ */
/* -------- Forward references for procedures: --------------------------- */
/* -------- Declarations: ------------------------------------------------ */
/* -------- Definitions: ------------------------------------------------- */
/* -------- Data: -------------------------------------------------------- */
/* -------- Strings: ----------------------------------------------------- */
/* -------- Implementation: ---------------------------------------------- */
/*
* Convert hex string to long.
*
*/
unsigned long long hextoll(char *str)
{
unsigned long long ret;
sscanf(str, "%llx", &ret);
return ret;
}
/*
* Convert hex string to long.
*
*/
ulong bintol(char *str)
{
ulong sum = 0L;
char chh;
while ((chh = *str) == '1' || chh == '0')
{
sum *= 2; /* make the one higher */
if(chh == '1')
sum++;
str++;
}
return(sum);
}
/*
* Convert octal string to long.
*
*/
ulong otol(char *str)
{
ulong sum = 0L;
char chh;
while ((chh = *str) >= '0' && chh <= '7')
{
sum *= 8; /* make the one higher */
sum += chh - '0';
str++;
}
return(sum);
}
uint hextoi(char *str, int lim)
{
unsigned int sum = 0;
char chh, val;
while (1)
{
chh = *str;
if(!isxdigit(chh))
break;
if(!lim)
break;
sum *= 0x10;
if (isdigit(chh))
val = chh - '0';
else
{
chh = tolower(chh);
val = chh - 'a' + 10;
}
sum += val;
str++; lim--;
}
#ifdef PG_DEBUG
printf("SUM: %x\n", sum);
#endif
return(sum);
}
uint dectoi(char *str, int lim)
{
unsigned int sum = 0;
char chh, val;
while (1)
{
chh = *str;
if(!isdigit(chh))
break;
if(!lim)
break;
sum *= 10;
val = chh - '0';
sum += val;
str++; lim--;
}
#ifdef PG_DEBUG
printf("DEC SUM: %x\n", sum);
#endif
return(sum);
}
uint hexlen(char *str)
{
unsigned int sum = 0;
while (1)
{
char chh = *str;
if(!isxdigit(chh))
break;
str++; sum++;
}
#ifdef PG_DEBUG
printf("LEN: %x\n", sum);
#endif
return(sum);
}
uint declen(char *str)
{
unsigned int sum = 0;
while (1)
{
char chh = *str;
if(!isdigit(chh))
break;
str++; sum++;
}
#ifdef PG_DEBUG
printf("LEN: %x\n", sum);
#endif
return(sum);
}
/*
* Convert an unsigned long to a string representation in binary
*
*/
extern int fNibble;
void long_to_bin_str(unsigned long long num, char *str)
{
size_t i;
char tmp_str[sizeof(num) * 8 * 2 + 1];
char *p = tmp_str + sizeof(tmp_str) - 1;
if (num == 0) /* dummy result */
{
strcpy(str, "0");
return;
}
*p-- = '\0'; /* terminate */
for (i = 0; i < sizeof(num) * 8; ++i)
{
unsigned long long bit = (unsigned long long)1 << i;
*p-- = (num & bit ? '1' : '0');
if (fNibble && (i + 1) % 4 == 0)
*p-- = ' ';
}
*p = ' ';
while (*p == '0' || *p == ' ') /* skip leading zeros */
++p;
if (fNibble) /* fill out leading nibble */
{
for (i = 1; p[i] && p[i] != ' '; ++i)
continue;
while (i++ < 4)
*--p = '0';
}
strcpy(str, p); /* output result */
}
/* EOF */