-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntToString.cpp
191 lines (167 loc) · 3.43 KB
/
IntToString.cpp
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
// Common/IntToString.cpp
#include "CpuArch.h"
#include "IntToString.h"
#define CONVERT_INT_TO_STR(charType, tempSize) \
unsigned char temp[tempSize]; unsigned i = 0; \
while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
*s++ = (charType)('0' + (unsigned)val); \
while (i != 0) { i--; *s++ = temp[i]; } \
*s = 0;
void ConvertUInt32ToString(UInt32 val, char *s) throw()
{
CONVERT_INT_TO_STR(char, 16);
}
void ConvertUInt64ToString(UInt64 val, char *s) throw()
{
if (val <= (UInt32)0xFFFFFFFF)
{
ConvertUInt32ToString((UInt32)val, s);
return;
}
CONVERT_INT_TO_STR(char, 24);
}
void ConvertUInt64ToOct(UInt64 val, char *s) throw()
{
UInt64 v = val;
unsigned i;
for (i = 1;; i++)
{
v >>= 3;
if (v == 0)
break;
}
s[i] = 0;
do
{
unsigned t = (unsigned)(val & 0x7);
val >>= 3;
s[--i] = (char)('0' + t);
}
while (i);
}
#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10)))))
static inline char GetHexChar(unsigned t) { return GET_HEX_CHAR(t); }
void ConvertUInt32ToHex(UInt32 val, char *s) throw()
{
UInt32 v = val;
unsigned i;
for (i = 1;; i++)
{
v >>= 4;
if (v == 0)
break;
}
s[i] = 0;
do
{
unsigned t = (unsigned)(val & 0xF);
val >>= 4;
s[--i] = GET_HEX_CHAR(t);
}
while (i);
}
void ConvertUInt64ToHex(UInt64 val, char *s) throw()
{
UInt64 v = val;
unsigned i;
for (i = 1;; i++)
{
v >>= 4;
if (v == 0)
break;
}
s[i] = 0;
do
{
unsigned t = (unsigned)(val & 0xF);
val >>= 4;
s[--i] = GET_HEX_CHAR(t);
}
while (i);
}
void ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw()
{
s[8] = 0;
for (int i = 7; i >= 0; i--)
{
unsigned t = val & 0xF;
val >>= 4;
s[i] = GET_HEX_CHAR(t);;
}
}
/*
void ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s)
{
s[8] = 0;
for (int i = 7; i >= 0; i--)
{
unsigned t = val & 0xF;
val >>= 4;
s[i] = (wchar_t)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
}
}
*/
void ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
{
CONVERT_INT_TO_STR(wchar_t, 16);
}
void ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
{
if (val <= (UInt32)0xFFFFFFFF)
{
ConvertUInt32ToString((UInt32)val, s);
return;
}
CONVERT_INT_TO_STR(wchar_t, 24);
}
void ConvertInt64ToString(Int64 val, char *s) throw()
{
if (val < 0)
{
*s++ = '-';
val = -val;
}
ConvertUInt64ToString(val, s);
}
void ConvertInt64ToString(Int64 val, wchar_t *s) throw()
{
if (val < 0)
{
*s++ = L'-';
val = -val;
}
ConvertUInt64ToString(val, s);
}
static void ConvertByteToHex2Digits(unsigned v, char *s) throw()
{
s[0] = GetHexChar(v >> 4);
s[1] = GetHexChar(v & 0xF);
}
static void ConvertUInt16ToHex4Digits(UInt32 val, char *s) throw()
{
ConvertByteToHex2Digits(val >> 8, s);
ConvertByteToHex2Digits(val & 0xFF, s + 2);
}
char *RawLeGuidToString(const Byte *g, char *s) throw()
{
ConvertUInt32ToHex8Digits(GetUi32(g ), s); s += 8; *s++ = '-';
ConvertUInt16ToHex4Digits(GetUi16(g + 4), s); s += 4; *s++ = '-';
ConvertUInt16ToHex4Digits(GetUi16(g + 6), s); s += 4; *s++ = '-';
for (unsigned i = 0; i < 8; i++)
{
if (i == 2)
*s++ = '-';
ConvertByteToHex2Digits(g[8 + i], s);
s += 2;
}
*s = 0;
return s;
}
char *RawLeGuidToString_Braced(const Byte *g, char *s) throw()
{
*s++ = '{';
s = RawLeGuidToString(g, s);
*s++ = '}';
*s = 0;
return s;
}