-
Notifications
You must be signed in to change notification settings - Fork 23
/
convert.c
178 lines (161 loc) · 3.64 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
// Last Modification: 03-06-2009
#include <stdio.h>
#include "convert.h"
///////////////////////////////////////////////////////////////////////////////
void bin8(unsigned char n, char *s)
{
int i;
for ( i=7; i>=0; i-- )
{
*s = '0' + ( (n >> i)& 1 );
s++;
}
*s='\0';
}
///////////////////////////////////////////////////////////////////////////////
void bin16(unsigned short n, char *s)
{
int i;
for ( i=15; i>=0; i-- )
{
*s = '0' + ( (n >> i)& 1 );
s++;
}
*s='\0';
}
///////////////////////////////////////////////////////////////////////////////
void bin32(unsigned short n, char *s)
{
int i;
for ( i=31; i>=0; i-- )
{
*s = '0' + ( (n >> i)& 1 );
s++;
}
*s='\0';
}
///////////////////////////////////////////////////////////////////////////////
void array2bin( char *src, char *dest, int count )
{
int i,j;
char c;
for ( i=0; i<count; i++)
{
c = *src;
for ( j=7; j>=0; j-- )
{
*dest = '0' + ( (c >> j)& 1 ); // if ( (c >> j)&1 ) *dest = '1'; else *dest='0';
dest++;
}
*dest=' '; dest++;
src++;
}
*dest='\0';
}
///////////////////////////////////////////////////////////////////////////////
char hexchars[]="0123456789ABCDEF";
void hex8(int n, char *s)
{
int i;
for( i=4; i>=0; i-=4 )
{
*s = hexchars[ (n>>i) & 0x0F ];
s++;
}
*s = '\0';
}
///////////////////////////////////////////////////////////////////////////////
void hex16(int n, char *s)
{
int i;
for( i=12; i>=0; i-=4 )
{
*s = hexchars[ (n>>i) & 0x0F ];
s++;
}
*s = '\0';
}
///////////////////////////////////////////////////////////////////////////////
void hex32(int n, char *s)
{
int i;
for( i=28; i>=0; i-=4 ) {
*s = hexchars[ (n>>i) & 0x0F ];
s++;
}
*s = '\0';
}
///////////////////////////////////////////////////////////////////////////////
char *array2hex( unsigned char *src, char *dest, int count )
{
int i;
unsigned char c;
for ( i=0; i<count; i++) {
c = *src;
*dest = hexchars[ (c>>4) & 0x0F ]; dest++;
*dest = hexchars[ c & 0x0F ]; dest++;
*dest = ' '; dest++;
src++;
}
*dest='\0';
return dest;
}
///////////////////////////////////////////////////////////////////////////////
int hexvalue(char src)
{
int rt=0;
if ( (src>='0')&&(src<='9') ) rt = (rt<<4) + (src-'0');
else if ( (src>='a')&&(src<='f') ) rt = (rt<<4) + (src-'a')+10;
else if ( (src>='A')&&(src<='F') ) rt = (rt<<4) + (src-'A')+10;
else return -1;
return rt;
}
///////////////////////////////////////////////////////////////////////////////
int hex2int(char *src)
{
int rt=0;
while (1)
{
if ( (*src>='0')&&(*src<='9') ) rt = (rt<<4) + (*src-'0');
else if ( (*src>='a')&&(*src<='f') ) rt = (rt<<4) + (*src-'a')+10;
else if ( (*src>='A')&&(*src<='F') ) rt = (rt<<4) + (*src-'A')+10;
else break;
src++;
}
return ( rt );
}
///////////////////////////////////////////////////////////////////////////////
int hex2array( char *src, unsigned char *dest)
{
unsigned int rt=0;
unsigned char c;
int size=0;
int chars=0;
do
{
c=*src;
if ( (c>='0')&&(c<='9') ) { rt = (rt<<4) + (c-'0'); chars++; }
else if ( (c>='a')&&(c<='f') ) { rt = (rt<<4) + (c-'a')+10; chars++; }
else if ( (c>='A')&&(c<='F') ) { rt = (rt<<4) + (c-'A')+10; chars++; }
else if (chars>0)
{
*dest = rt;
dest++;
size++;
rt=0;
chars=0;
}
if (chars==2)
{
*dest = rt;
dest++;
size++;
rt=0;
chars=0;
}
src++;
} while (c!='\0');
return size;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////