1 // Common/IntToString.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../C/CpuArch.h"
6
7 #include "IntToString.h"
8
9 #define CONVERT_INT_TO_STR(charType, tempSize) \
10 unsigned char temp[tempSize]; unsigned i = 0; \
11 while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
12 *s++ = (charType)('0' + (unsigned)val); \
13 while (i != 0) { i--; *s++ = (charType)temp[i]; } \
14 *s = 0; \
15 return s;
16
ConvertUInt32ToString(UInt32 val,char * s)17 char * ConvertUInt32ToString(UInt32 val, char *s) throw()
18 {
19 CONVERT_INT_TO_STR(char, 16)
20 }
21
ConvertUInt64ToString(UInt64 val,char * s)22 char * ConvertUInt64ToString(UInt64 val, char *s) throw()
23 {
24 if (val <= (UInt32)0xFFFFFFFF)
25 {
26 return ConvertUInt32ToString((UInt32)val, s);
27 }
28 CONVERT_INT_TO_STR(char, 24)
29 }
30
ConvertUInt64ToOct(UInt64 val,char * s)31 void ConvertUInt64ToOct(UInt64 val, char *s) throw()
32 {
33 UInt64 v = val;
34 unsigned i;
35 for (i = 1;; i++)
36 {
37 v >>= 3;
38 if (v == 0)
39 break;
40 }
41 s[i] = 0;
42 do
43 {
44 unsigned t = (unsigned)(val & 0x7);
45 val >>= 3;
46 s[--i] = (char)('0' + t);
47 }
48 while (i);
49 }
50
51
52 #define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10)))))
53
GetHexChar(unsigned t)54 static inline char GetHexChar(unsigned t) { return GET_HEX_CHAR(t); }
55
56
ConvertUInt32ToHex(UInt32 val,char * s)57 void ConvertUInt32ToHex(UInt32 val, char *s) throw()
58 {
59 UInt32 v = val;
60 unsigned i;
61 for (i = 1;; i++)
62 {
63 v >>= 4;
64 if (v == 0)
65 break;
66 }
67 s[i] = 0;
68 do
69 {
70 unsigned t = (unsigned)(val & 0xF);
71 val >>= 4;
72 s[--i] = GET_HEX_CHAR(t);
73 }
74 while (i);
75 }
76
77
ConvertUInt64ToHex(UInt64 val,char * s)78 void ConvertUInt64ToHex(UInt64 val, char *s) throw()
79 {
80 UInt64 v = val;
81 unsigned i;
82 for (i = 1;; i++)
83 {
84 v >>= 4;
85 if (v == 0)
86 break;
87 }
88 s[i] = 0;
89 do
90 {
91 unsigned t = (unsigned)(val & 0xF);
92 val >>= 4;
93 s[--i] = GET_HEX_CHAR(t);
94 }
95 while (i);
96 }
97
ConvertUInt32ToHex8Digits(UInt32 val,char * s)98 void ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw()
99 {
100 s[8] = 0;
101 for (int i = 7; i >= 0; i--)
102 {
103 unsigned t = val & 0xF;
104 val >>= 4;
105 s[i] = GET_HEX_CHAR(t);
106 }
107 }
108
109 /*
110 void ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s)
111 {
112 s[8] = 0;
113 for (int i = 7; i >= 0; i--)
114 {
115 unsigned t = val & 0xF;
116 val >>= 4;
117 s[i] = (wchar_t)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
118 }
119 }
120 */
121
ConvertUInt32ToString(UInt32 val,wchar_t * s)122 wchar_t * ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
123 {
124 CONVERT_INT_TO_STR(wchar_t, 16)
125 }
126
ConvertUInt64ToString(UInt64 val,wchar_t * s)127 wchar_t * ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
128 {
129 if (val <= (UInt32)0xFFFFFFFF)
130 {
131 return ConvertUInt32ToString((UInt32)val, s);
132 }
133 CONVERT_INT_TO_STR(wchar_t, 24)
134 }
135
ConvertInt64ToString(Int64 val,char * s)136 void ConvertInt64ToString(Int64 val, char *s) throw()
137 {
138 if (val < 0)
139 {
140 *s++ = '-';
141 val = -val;
142 }
143 ConvertUInt64ToString((UInt64)val, s);
144 }
145
ConvertInt64ToString(Int64 val,wchar_t * s)146 void ConvertInt64ToString(Int64 val, wchar_t *s) throw()
147 {
148 if (val < 0)
149 {
150 *s++ = L'-';
151 val = -val;
152 }
153 ConvertUInt64ToString((UInt64)val, s);
154 }
155
156
ConvertByteToHex2Digits(unsigned v,char * s)157 static void ConvertByteToHex2Digits(unsigned v, char *s) throw()
158 {
159 s[0] = GetHexChar(v >> 4);
160 s[1] = GetHexChar(v & 0xF);
161 }
162
ConvertUInt16ToHex4Digits(UInt32 val,char * s)163 static void ConvertUInt16ToHex4Digits(UInt32 val, char *s) throw()
164 {
165 ConvertByteToHex2Digits(val >> 8, s);
166 ConvertByteToHex2Digits(val & 0xFF, s + 2);
167 }
168
RawLeGuidToString(const Byte * g,char * s)169 char *RawLeGuidToString(const Byte *g, char *s) throw()
170 {
171 ConvertUInt32ToHex8Digits(GetUi32(g ), s); s += 8; *s++ = '-';
172 ConvertUInt16ToHex4Digits(GetUi16(g + 4), s); s += 4; *s++ = '-';
173 ConvertUInt16ToHex4Digits(GetUi16(g + 6), s); s += 4; *s++ = '-';
174 for (unsigned i = 0; i < 8; i++)
175 {
176 if (i == 2)
177 *s++ = '-';
178 ConvertByteToHex2Digits(g[8 + i], s);
179 s += 2;
180 }
181 *s = 0;
182 return s;
183 }
184
RawLeGuidToString_Braced(const Byte * g,char * s)185 char *RawLeGuidToString_Braced(const Byte *g, char *s) throw()
186 {
187 *s++ = '{';
188 s = RawLeGuidToString(g, s);
189 *s++ = '}';
190 *s = 0;
191 return s;
192 }
193