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 if (val < 10) \
11 *s++ = (charType)('0' + (unsigned)val); \
12 else { \
13 Byte temp[tempSize]; \
14 size_t i = 0; \
15 do { \
16 temp[++i] = (Byte)('0' + (unsigned)(val % 10)); \
17 val /= 10; } \
18 while (val >= 10); \
19 *s++ = (charType)('0' + (unsigned)val); \
20 do { *s++ = (charType)temp[i]; } \
21 while (--i); \
22 } \
23 *s = 0; \
24 return s;
25
ConvertUInt32ToString(UInt32 val,char * s)26 char * ConvertUInt32ToString(UInt32 val, char *s) throw()
27 {
28 CONVERT_INT_TO_STR(char, 16)
29 }
30
ConvertUInt64ToString(UInt64 val,char * s)31 char * ConvertUInt64ToString(UInt64 val, char *s) throw()
32 {
33 if (val <= (UInt32)0xFFFFFFFF)
34 return ConvertUInt32ToString((UInt32)val, s);
35 CONVERT_INT_TO_STR(char, 24)
36 }
37
ConvertUInt32ToString(UInt32 val,wchar_t * s)38 wchar_t * ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
39 {
40 CONVERT_INT_TO_STR(wchar_t, 16)
41 }
42
ConvertUInt64ToString(UInt64 val,wchar_t * s)43 wchar_t * ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
44 {
45 if (val <= (UInt32)0xFFFFFFFF)
46 return ConvertUInt32ToString((UInt32)val, s);
47 CONVERT_INT_TO_STR(wchar_t, 24)
48 }
49
ConvertInt64ToString(Int64 val,char * s)50 void ConvertInt64ToString(Int64 val, char *s) throw()
51 {
52 if (val < 0)
53 {
54 *s++ = '-';
55 val = -val;
56 }
57 ConvertUInt64ToString((UInt64)val, s);
58 }
59
ConvertInt64ToString(Int64 val,wchar_t * s)60 void ConvertInt64ToString(Int64 val, wchar_t *s) throw()
61 {
62 if (val < 0)
63 {
64 *s++ = L'-';
65 val = -val;
66 }
67 ConvertUInt64ToString((UInt64)val, s);
68 }
69
70
ConvertUInt64ToOct(UInt64 val,char * s)71 void ConvertUInt64ToOct(UInt64 val, char *s) throw()
72 {
73 {
74 UInt64 v = val;
75 do
76 s++;
77 while (v >>= 3);
78 }
79 *s = 0;
80 do
81 {
82 const unsigned t = (unsigned)val & 7;
83 *--s = (char)('0' + t);
84 }
85 while (val >>= 3);
86 }
87
88 MY_ALIGN(16) const char k_Hex_Upper[16] =
89 { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
90 MY_ALIGN(16) const char k_Hex_Lower[16] =
91 { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
92
ConvertUInt32ToHex(UInt32 val,char * s)93 void ConvertUInt32ToHex(UInt32 val, char *s) throw()
94 {
95 {
96 UInt32 v = val;
97 do
98 s++;
99 while (v >>= 4);
100 }
101 *s = 0;
102 do
103 {
104 const unsigned t = (unsigned)val & 0xF;
105 *--s = GET_HEX_CHAR_UPPER(t);
106 }
107 while (val >>= 4);
108 }
109
ConvertUInt64ToHex(UInt64 val,char * s)110 void ConvertUInt64ToHex(UInt64 val, char *s) throw()
111 {
112 {
113 UInt64 v = val;
114 do
115 s++;
116 while (v >>= 4);
117 }
118 *s = 0;
119 do
120 {
121 const unsigned t = (unsigned)val & 0xF;
122 *--s = GET_HEX_CHAR_UPPER(t);
123 }
124 while (val >>= 4);
125 }
126
ConvertUInt32ToHex8Digits(UInt32 val,char * s)127 void ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw()
128 {
129 s[8] = 0;
130 int i = 7;
131 do
132 {
133 { const unsigned t = (unsigned)val & 0xF; s[i--] = GET_HEX_CHAR_UPPER(t); }
134 { const unsigned t = (Byte)val >> 4; val >>= 8; s[i--] = GET_HEX_CHAR_UPPER(t); }
135 }
136 while (i >= 0);
137 }
138
139 /*
140 void ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s)
141 {
142 s[8] = 0;
143 for (int i = 7; i >= 0; i--)
144 {
145 const unsigned t = (unsigned)val & 0xF;
146 val >>= 4;
147 s[i] = GET_HEX_CHAR(t);
148 }
149 }
150 */
151
152
153 MY_ALIGN(16) static const Byte k_Guid_Pos[] =
154 { 6,4,2,0, 11,9, 16,14, 19,21, 24,26,28,30,32,34 };
155
RawLeGuidToString(const Byte * g,char * s)156 char *RawLeGuidToString(const Byte *g, char *s) throw()
157 {
158 s[ 8] = '-';
159 s[13] = '-';
160 s[18] = '-';
161 s[23] = '-';
162 s[36] = 0;
163 for (unsigned i = 0; i < 16; i++)
164 {
165 char *s2 = s + k_Guid_Pos[i];
166 const unsigned v = g[i];
167 s2[0] = GET_HEX_CHAR_UPPER(v >> 4);
168 s2[1] = GET_HEX_CHAR_UPPER(v & 0xF);
169 }
170 return s + 36;
171 }
172
RawLeGuidToString_Braced(const Byte * g,char * s)173 char *RawLeGuidToString_Braced(const Byte *g, char *s) throw()
174 {
175 *s++ = '{';
176 s = RawLeGuidToString(g, s);
177 *s++ = '}';
178 *s = 0;
179 return s;
180 }
181
182
ConvertDataToHex_Lower(char * dest,const Byte * src,size_t size)183 void ConvertDataToHex_Lower(char *dest, const Byte *src, size_t size) throw()
184 {
185 if (size)
186 {
187 const Byte *lim = src + size;
188 do
189 {
190 const unsigned b = *src++;
191 dest[0] = GET_HEX_CHAR_LOWER(b >> 4);
192 dest[1] = GET_HEX_CHAR_LOWER(b & 0xF);
193 dest += 2;
194 }
195 while (src != lim);
196 }
197 *dest = 0;
198 }
199
ConvertDataToHex_Upper(char * dest,const Byte * src,size_t size)200 void ConvertDataToHex_Upper(char *dest, const Byte *src, size_t size) throw()
201 {
202 if (size)
203 {
204 const Byte *lim = src + size;
205 do
206 {
207 const unsigned b = *src++;
208 dest[0] = GET_HEX_CHAR_UPPER(b >> 4);
209 dest[1] = GET_HEX_CHAR_UPPER(b & 0xF);
210 dest += 2;
211 }
212 while (src != lim);
213 }
214 *dest = 0;
215 }
216