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