1 // Common/IntToString.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "IntToString.h"
6 
7 #define CONVERT_INT_TO_STR(charType, tempSize) \
8   unsigned char temp[tempSize]; unsigned i = 0; \
9   while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
10   *s++ = (charType)('0' + (unsigned)val); \
11   while (i != 0) { i--; *s++ = temp[i]; } \
12   *s = 0;
13 
ConvertUInt32ToString(UInt32 val,char * s)14 void ConvertUInt32ToString(UInt32 val, char *s) throw()
15 {
16   CONVERT_INT_TO_STR(char, 16);
17 }
18 
ConvertUInt64ToString(UInt64 val,char * s)19 void ConvertUInt64ToString(UInt64 val, char *s) throw()
20 {
21   if (val <= (UInt32)0xFFFFFFFF)
22   {
23     ConvertUInt32ToString((UInt32)val, s);
24     return;
25   }
26   CONVERT_INT_TO_STR(char, 24);
27 }
28 
ConvertUInt64ToOct(UInt64 val,char * s)29 void ConvertUInt64ToOct(UInt64 val, char *s) throw()
30 {
31   UInt64 v = val;
32   unsigned i;
33   for (i = 1;; i++)
34   {
35     v >>= 3;
36     if (v == 0)
37       break;
38   }
39   s[i] = 0;
40   do
41   {
42     unsigned t = (unsigned)(val & 0x7);
43     val >>= 3;
44     s[--i] = (char)('0' + t);
45   }
46   while (i);
47 }
48 
ConvertUInt32ToHex(UInt32 val,char * s)49 void ConvertUInt32ToHex(UInt32 val, char *s) throw()
50 {
51   UInt32 v = val;
52   unsigned i;
53   for (i = 1;; i++)
54   {
55     v >>= 4;
56     if (v == 0)
57       break;
58   }
59   s[i] = 0;
60   do
61   {
62     unsigned t = (unsigned)((val & 0xF));
63     val >>= 4;
64     s[--i] = (char)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
65   }
66   while (i);
67 }
68 
ConvertUInt64ToHex(UInt64 val,char * s)69 void ConvertUInt64ToHex(UInt64 val, char *s) throw()
70 {
71   UInt64 v = val;
72   unsigned i;
73   for (i = 1;; i++)
74   {
75     v >>= 4;
76     if (v == 0)
77       break;
78   }
79   s[i] = 0;
80   do
81   {
82     unsigned t = (unsigned)((val & 0xF));
83     val >>= 4;
84     s[--i] = (char)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
85   }
86   while (i);
87 }
88 
ConvertUInt32ToHex8Digits(UInt32 val,char * s)89 void ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw()
90 {
91   s[8] = 0;
92   for (int i = 7; i >= 0; i--)
93   {
94     unsigned t = val & 0xF;
95     val >>= 4;
96     s[i] = (char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
97   }
98 }
99 
100 /*
101 void ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s)
102 {
103   s[8] = 0;
104   for (int i = 7; i >= 0; i--)
105   {
106     unsigned t = val & 0xF;
107     val >>= 4;
108     s[i] = (wchar_t)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
109   }
110 }
111 */
112 
ConvertUInt32ToString(UInt32 val,wchar_t * s)113 void ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
114 {
115   CONVERT_INT_TO_STR(wchar_t, 16);
116 }
117 
ConvertUInt64ToString(UInt64 val,wchar_t * s)118 void ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
119 {
120   if (val <= (UInt32)0xFFFFFFFF)
121   {
122     ConvertUInt32ToString((UInt32)val, s);
123     return;
124   }
125   CONVERT_INT_TO_STR(wchar_t, 24);
126 }
127 
ConvertInt64ToString(Int64 val,char * s)128 void ConvertInt64ToString(Int64 val, char *s) throw()
129 {
130   if (val < 0)
131   {
132     *s++ = '-';
133     val = -val;
134   }
135   ConvertUInt64ToString(val, s);
136 }
137 
ConvertInt64ToString(Int64 val,wchar_t * s)138 void ConvertInt64ToString(Int64 val, wchar_t *s) throw()
139 {
140   if (val < 0)
141   {
142     *s++ = L'-';
143     val = -val;
144   }
145   ConvertUInt64ToString(val, s);
146 }
147