1370b324cSopenharmony_ci// Common/IntToString.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "../../C/CpuArch.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#include "IntToString.h"
8370b324cSopenharmony_ci
9370b324cSopenharmony_ci#define CONVERT_INT_TO_STR(charType, tempSize) \
10370b324cSopenharmony_ci  unsigned char temp[tempSize]; unsigned i = 0; \
11370b324cSopenharmony_ci  while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
12370b324cSopenharmony_ci  *s++ = (charType)('0' + (unsigned)val); \
13370b324cSopenharmony_ci  while (i != 0) { i--; *s++ = (charType)temp[i]; } \
14370b324cSopenharmony_ci  *s = 0; \
15370b324cSopenharmony_ci  return s;
16370b324cSopenharmony_ci
17370b324cSopenharmony_cichar * ConvertUInt32ToString(UInt32 val, char *s) throw()
18370b324cSopenharmony_ci{
19370b324cSopenharmony_ci  CONVERT_INT_TO_STR(char, 16)
20370b324cSopenharmony_ci}
21370b324cSopenharmony_ci
22370b324cSopenharmony_cichar * ConvertUInt64ToString(UInt64 val, char *s) throw()
23370b324cSopenharmony_ci{
24370b324cSopenharmony_ci  if (val <= (UInt32)0xFFFFFFFF)
25370b324cSopenharmony_ci  {
26370b324cSopenharmony_ci    return ConvertUInt32ToString((UInt32)val, s);
27370b324cSopenharmony_ci  }
28370b324cSopenharmony_ci  CONVERT_INT_TO_STR(char, 24)
29370b324cSopenharmony_ci}
30370b324cSopenharmony_ci
31370b324cSopenharmony_civoid ConvertUInt64ToOct(UInt64 val, char *s) throw()
32370b324cSopenharmony_ci{
33370b324cSopenharmony_ci  UInt64 v = val;
34370b324cSopenharmony_ci  unsigned i;
35370b324cSopenharmony_ci  for (i = 1;; i++)
36370b324cSopenharmony_ci  {
37370b324cSopenharmony_ci    v >>= 3;
38370b324cSopenharmony_ci    if (v == 0)
39370b324cSopenharmony_ci      break;
40370b324cSopenharmony_ci  }
41370b324cSopenharmony_ci  s[i] = 0;
42370b324cSopenharmony_ci  do
43370b324cSopenharmony_ci  {
44370b324cSopenharmony_ci    unsigned t = (unsigned)(val & 0x7);
45370b324cSopenharmony_ci    val >>= 3;
46370b324cSopenharmony_ci    s[--i] = (char)('0' + t);
47370b324cSopenharmony_ci  }
48370b324cSopenharmony_ci  while (i);
49370b324cSopenharmony_ci}
50370b324cSopenharmony_ci
51370b324cSopenharmony_ci
52370b324cSopenharmony_ci#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10)))))
53370b324cSopenharmony_ci
54370b324cSopenharmony_cistatic inline char GetHexChar(unsigned t) { return GET_HEX_CHAR(t); }
55370b324cSopenharmony_ci
56370b324cSopenharmony_ci
57370b324cSopenharmony_civoid ConvertUInt32ToHex(UInt32 val, char *s) throw()
58370b324cSopenharmony_ci{
59370b324cSopenharmony_ci  UInt32 v = val;
60370b324cSopenharmony_ci  unsigned i;
61370b324cSopenharmony_ci  for (i = 1;; i++)
62370b324cSopenharmony_ci  {
63370b324cSopenharmony_ci    v >>= 4;
64370b324cSopenharmony_ci    if (v == 0)
65370b324cSopenharmony_ci      break;
66370b324cSopenharmony_ci  }
67370b324cSopenharmony_ci  s[i] = 0;
68370b324cSopenharmony_ci  do
69370b324cSopenharmony_ci  {
70370b324cSopenharmony_ci    unsigned t = (unsigned)(val & 0xF);
71370b324cSopenharmony_ci    val >>= 4;
72370b324cSopenharmony_ci    s[--i] = GET_HEX_CHAR(t);
73370b324cSopenharmony_ci  }
74370b324cSopenharmony_ci  while (i);
75370b324cSopenharmony_ci}
76370b324cSopenharmony_ci
77370b324cSopenharmony_ci
78370b324cSopenharmony_civoid ConvertUInt64ToHex(UInt64 val, char *s) throw()
79370b324cSopenharmony_ci{
80370b324cSopenharmony_ci  UInt64 v = val;
81370b324cSopenharmony_ci  unsigned i;
82370b324cSopenharmony_ci  for (i = 1;; i++)
83370b324cSopenharmony_ci  {
84370b324cSopenharmony_ci    v >>= 4;
85370b324cSopenharmony_ci    if (v == 0)
86370b324cSopenharmony_ci      break;
87370b324cSopenharmony_ci  }
88370b324cSopenharmony_ci  s[i] = 0;
89370b324cSopenharmony_ci  do
90370b324cSopenharmony_ci  {
91370b324cSopenharmony_ci    unsigned t = (unsigned)(val & 0xF);
92370b324cSopenharmony_ci    val >>= 4;
93370b324cSopenharmony_ci    s[--i] = GET_HEX_CHAR(t);
94370b324cSopenharmony_ci  }
95370b324cSopenharmony_ci  while (i);
96370b324cSopenharmony_ci}
97370b324cSopenharmony_ci
98370b324cSopenharmony_civoid ConvertUInt32ToHex8Digits(UInt32 val, char *s) throw()
99370b324cSopenharmony_ci{
100370b324cSopenharmony_ci  s[8] = 0;
101370b324cSopenharmony_ci  for (int i = 7; i >= 0; i--)
102370b324cSopenharmony_ci  {
103370b324cSopenharmony_ci    unsigned t = val & 0xF;
104370b324cSopenharmony_ci    val >>= 4;
105370b324cSopenharmony_ci    s[i] = GET_HEX_CHAR(t);
106370b324cSopenharmony_ci  }
107370b324cSopenharmony_ci}
108370b324cSopenharmony_ci
109370b324cSopenharmony_ci/*
110370b324cSopenharmony_civoid ConvertUInt32ToHex8Digits(UInt32 val, wchar_t *s)
111370b324cSopenharmony_ci{
112370b324cSopenharmony_ci  s[8] = 0;
113370b324cSopenharmony_ci  for (int i = 7; i >= 0; i--)
114370b324cSopenharmony_ci  {
115370b324cSopenharmony_ci    unsigned t = val & 0xF;
116370b324cSopenharmony_ci    val >>= 4;
117370b324cSopenharmony_ci    s[i] = (wchar_t)(((t < 10) ? ('0' + t) : ('A' + (t - 10))));
118370b324cSopenharmony_ci  }
119370b324cSopenharmony_ci}
120370b324cSopenharmony_ci*/
121370b324cSopenharmony_ci
122370b324cSopenharmony_ciwchar_t * ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
123370b324cSopenharmony_ci{
124370b324cSopenharmony_ci  CONVERT_INT_TO_STR(wchar_t, 16)
125370b324cSopenharmony_ci}
126370b324cSopenharmony_ci
127370b324cSopenharmony_ciwchar_t * ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
128370b324cSopenharmony_ci{
129370b324cSopenharmony_ci  if (val <= (UInt32)0xFFFFFFFF)
130370b324cSopenharmony_ci  {
131370b324cSopenharmony_ci    return ConvertUInt32ToString((UInt32)val, s);
132370b324cSopenharmony_ci  }
133370b324cSopenharmony_ci  CONVERT_INT_TO_STR(wchar_t, 24)
134370b324cSopenharmony_ci}
135370b324cSopenharmony_ci
136370b324cSopenharmony_civoid ConvertInt64ToString(Int64 val, char *s) throw()
137370b324cSopenharmony_ci{
138370b324cSopenharmony_ci  if (val < 0)
139370b324cSopenharmony_ci  {
140370b324cSopenharmony_ci    *s++ = '-';
141370b324cSopenharmony_ci    val = -val;
142370b324cSopenharmony_ci  }
143370b324cSopenharmony_ci  ConvertUInt64ToString((UInt64)val, s);
144370b324cSopenharmony_ci}
145370b324cSopenharmony_ci
146370b324cSopenharmony_civoid ConvertInt64ToString(Int64 val, wchar_t *s) throw()
147370b324cSopenharmony_ci{
148370b324cSopenharmony_ci  if (val < 0)
149370b324cSopenharmony_ci  {
150370b324cSopenharmony_ci    *s++ = L'-';
151370b324cSopenharmony_ci    val = -val;
152370b324cSopenharmony_ci  }
153370b324cSopenharmony_ci  ConvertUInt64ToString((UInt64)val, s);
154370b324cSopenharmony_ci}
155370b324cSopenharmony_ci
156370b324cSopenharmony_ci
157370b324cSopenharmony_cistatic void ConvertByteToHex2Digits(unsigned v, char *s) throw()
158370b324cSopenharmony_ci{
159370b324cSopenharmony_ci  s[0] = GetHexChar(v >> 4);
160370b324cSopenharmony_ci  s[1] = GetHexChar(v & 0xF);
161370b324cSopenharmony_ci}
162370b324cSopenharmony_ci
163370b324cSopenharmony_cistatic void ConvertUInt16ToHex4Digits(UInt32 val, char *s) throw()
164370b324cSopenharmony_ci{
165370b324cSopenharmony_ci  ConvertByteToHex2Digits(val >> 8, s);
166370b324cSopenharmony_ci  ConvertByteToHex2Digits(val & 0xFF, s + 2);
167370b324cSopenharmony_ci}
168370b324cSopenharmony_ci
169370b324cSopenharmony_cichar *RawLeGuidToString(const Byte *g, char *s) throw()
170370b324cSopenharmony_ci{
171370b324cSopenharmony_ci  ConvertUInt32ToHex8Digits(GetUi32(g   ),  s);  s += 8;  *s++ = '-';
172370b324cSopenharmony_ci  ConvertUInt16ToHex4Digits(GetUi16(g + 4), s);  s += 4;  *s++ = '-';
173370b324cSopenharmony_ci  ConvertUInt16ToHex4Digits(GetUi16(g + 6), s);  s += 4;  *s++ = '-';
174370b324cSopenharmony_ci  for (unsigned i = 0; i < 8; i++)
175370b324cSopenharmony_ci  {
176370b324cSopenharmony_ci    if (i == 2)
177370b324cSopenharmony_ci      *s++ = '-';
178370b324cSopenharmony_ci    ConvertByteToHex2Digits(g[8 + i], s);
179370b324cSopenharmony_ci    s += 2;
180370b324cSopenharmony_ci  }
181370b324cSopenharmony_ci  *s = 0;
182370b324cSopenharmony_ci  return s;
183370b324cSopenharmony_ci}
184370b324cSopenharmony_ci
185370b324cSopenharmony_cichar *RawLeGuidToString_Braced(const Byte *g, char *s) throw()
186370b324cSopenharmony_ci{
187370b324cSopenharmony_ci  *s++ = '{';
188370b324cSopenharmony_ci  s = RawLeGuidToString(g, s);
189370b324cSopenharmony_ci  *s++ = '}';
190370b324cSopenharmony_ci  *s = 0;
191370b324cSopenharmony_ci  return s;
192370b324cSopenharmony_ci}
193