Home | History | Annotate | Download | only in Common
      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++ = temp[i]; } \
     14   *s = 0;
     15 
     16 void ConvertUInt32ToString(UInt32 val, char *s) throw()
     17 {
     18   CONVERT_INT_TO_STR(char, 16);
     19 }
     20 
     21 void ConvertUInt64ToString(UInt64 val, char *s) throw()
     22 {
     23   if (val <= (UInt32)0xFFFFFFFF)
     24   {
     25     ConvertUInt32ToString((UInt32)val, s);
     26     return;
     27   }
     28   CONVERT_INT_TO_STR(char, 24);
     29 }
     30 
     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 
     54 static inline char GetHexChar(unsigned t) { return GET_HEX_CHAR(t); }
     55 
     56 
     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 
     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 
     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 
    122 void ConvertUInt32ToString(UInt32 val, wchar_t *s) throw()
    123 {
    124   CONVERT_INT_TO_STR(wchar_t, 16);
    125 }
    126 
    127 void ConvertUInt64ToString(UInt64 val, wchar_t *s) throw()
    128 {
    129   if (val <= (UInt32)0xFFFFFFFF)
    130   {
    131     ConvertUInt32ToString((UInt32)val, s);
    132     return;
    133   }
    134   CONVERT_INT_TO_STR(wchar_t, 24);
    135 }
    136 
    137 void ConvertInt64ToString(Int64 val, char *s) throw()
    138 {
    139   if (val < 0)
    140   {
    141     *s++ = '-';
    142     val = -val;
    143   }
    144   ConvertUInt64ToString(val, s);
    145 }
    146 
    147 void ConvertInt64ToString(Int64 val, wchar_t *s) throw()
    148 {
    149   if (val < 0)
    150   {
    151     *s++ = L'-';
    152     val = -val;
    153   }
    154   ConvertUInt64ToString(val, s);
    155 }
    156 
    157 
    158 static void ConvertByteToHex2Digits(unsigned v, char *s) throw()
    159 {
    160   s[0] = GetHexChar(v >> 4);
    161   s[1] = GetHexChar(v & 0xF);
    162 }
    163 
    164 static void ConvertUInt16ToHex4Digits(UInt32 val, char *s) throw()
    165 {
    166   ConvertByteToHex2Digits(val >> 8, s);
    167   ConvertByteToHex2Digits(val & 0xFF, s + 2);
    168 }
    169 
    170 char *RawLeGuidToString(const Byte *g, char *s) throw()
    171 {
    172   ConvertUInt32ToHex8Digits(GetUi32(g   ),  s);  s += 8;  *s++ = '-';
    173   ConvertUInt16ToHex4Digits(GetUi16(g + 4), s);  s += 4;  *s++ = '-';
    174   ConvertUInt16ToHex4Digits(GetUi16(g + 6), s);  s += 4;  *s++ = '-';
    175   for (unsigned i = 0; i < 8; i++)
    176   {
    177     if (i == 2)
    178       *s++ = '-';
    179     ConvertByteToHex2Digits(g[8 + i], s);
    180     s += 2;
    181   }
    182   *s = 0;
    183   return s;
    184 }
    185 
    186 char *RawLeGuidToString_Braced(const Byte *g, char *s) throw()
    187 {
    188   *s++ = '{';
    189   s = RawLeGuidToString(g, s);
    190   *s++ = '}';
    191   *s = 0;
    192   return s;
    193 }
    194