-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHex.c
211 lines (188 loc) · 4.1 KB
/
Hex.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
////////////////////////////////////////////////////////////////////////////////
//
// Global including files.
//
// Including the files for the system.
//
// These including files are generally used in different operation system.
//
////////////////////////////////////////////////////////////////////////////////
#include "Global.h"
////////////////////////////////////////////////////////////////////////////////
//
// Is hex characater.
//
////////////////////////////////////////////////////////////////////////////////
_BOOL IsHexCharacater(_CHAR cValue)
{
//Check value.
if(cValue >= '0' && cValue <= '9')
{
//Return true.
return _TRUE;
}
//Check value.
if(cValue >= 'a' && cValue <= 'z')
{
//Return true.
return _TRUE;
}
//Check value.
if(cValue >= 'A' && cValue <= 'Z')
{
//Return true.
return _TRUE;
}
//Return false.
return _FALSE;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get hex characater.
//
////////////////////////////////////////////////////////////////////////////////
_CHAR GetHexCharacater(_BYTE cValue)
{
//Check value.
if(cValue >= 0 && cValue <= 9)
{
//Add value.
cValue += '0';
}
//Check value.
else if(cValue >= 10 && cValue <= 15)
{
//Add value.
cValue += 'a' - 10;
}
else
{
#ifdef _DEBUG
PrintLine(stderr,"Hex::GetHexCharacater : value is outof range !");
#endif
return _FAILURE;
}
//Return value.
return cValue;
}
////////////////////////////////////////////////////////////////////////////////
//
// Get hex value.
//
////////////////////////////////////////////////////////////////////////////////
_BYTE GetHexValue(_CHAR cValue)
{
//Check value.
if(cValue >= '0' && cValue <= '9')
{
//Add value.
cValue -= '0';
}
//Check value.
else if(cValue >= 'a' && cValue <= 'z')
{
//Add value.
cValue -= 'a' - 10;
}
//Check value.
else if(cValue >= 'A' && cValue <= 'Z')
{
//Add value.
cValue -= 'A' - 10;
}
else
{
#ifdef _DEBUG
PrintLine(stderr,"Hex::GetHexValue : invalid hex characater !");
#endif
return _FAILURE;
}
//Return value.
return cValue;
}
////////////////////////////////////////////////////////////////////////////////
//
// To hex value.
//
////////////////////////////////////////////////////////////////////////////////
_BYTE ToHexValue(_WIDECHAR wValue)
{
_UINT32 i;
_BYTE bValue = 0;
_UINT32 nOffset = 0;
//Do while.
for(i = 0;i < sizeof(_WIDECHAR);i ++)
{
//Move left.
bValue <<= 4;
//Calculate offset.
nOffset = 8 * i;
//Get value.
bValue |= GetHexValue((_CHAR)(wValue >> nOffset));
}
//Return value.
return bValue;
}
////////////////////////////////////////////////////////////////////////////////
//
// To hex characater.
//
////////////////////////////////////////////////////////////////////////////////
_WIDECHAR ToHexCharacater(_BYTE bValue)
{
_UINT32 i;
_WIDECHAR wValue = 0;
_UINT32 nOffset = 0;
//Do while.
for(i = 0;i < sizeof(_WIDECHAR);i ++)
{
//Move left.
wValue <<= 8;
//Calculate offset.
nOffset = 4 * i;
//Get value.
wValue |= GetHexCharacater((_BYTE)((bValue >> nOffset) & 0x0F));
}
//Return value.
return wValue;
}
////////////////////////////////////////////////////////////////////////////////
//
// Hex format.
//
////////////////////////////////////////////////////////////////////////////////
_WIDESTRING HexFormat(_WIDESTRING lpwstrFormat,_BYTE* lpBuffer,_UINT32 nSize)
{
_UINT32 i;
#ifdef _DEBUG
assert(lpwstrFormat != NULL && lpBuffer != NULL);
#endif
//Format bytes.
for(i = 0;i < nSize;i ++)
{
//Format.
lpwstrFormat[i] = ToHexCharacater(lpBuffer[i]);
}
//Add pointer.
lpwstrFormat += nSize;
//Return pointer.
return lpwstrFormat;
}
////////////////////////////////////////////////////////////////////////////////
//
// Hex scan.
//
////////////////////////////////////////////////////////////////////////////////
void HexScan(_WIDESTRING lpwstrFormat,_BYTE* lpBuffer,_UINT32 nSize)
{
_UINT32 i;
#ifdef _DEBUG
assert(lpwstrFormat != NULL && lpBuffer != NULL);
#endif
//Scan bytes.
for(i = 0;i < nSize && i < strlen((_STRING)lpwstrFormat) / 2;i ++)
{
//Scan.
lpBuffer[i] = ToHexValue(lpwstrFormat[i]);
}
}