Home | History | Annotate | Download | only in Common
      1 // Common/IntToString.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include "IntToString.h"
      6 
      7 void ConvertUInt64ToString(UInt64 value, char *s, UInt32 base)
      8 {
      9   if (base < 2 || base > 36)
     10   {
     11     *s = '\0';
     12     return;
     13   }
     14   char temp[72];
     15   int pos = 0;
     16   do
     17   {
     18     int delta = (int)(value % base);
     19     temp[pos++] = (char)((delta < 10) ? ('0' + delta) : ('a' + (delta - 10)));
     20     value /= base;
     21   }
     22   while (value != 0);
     23   do
     24     *s++ = temp[--pos];
     25   while (pos > 0);
     26   *s = '\0';
     27 }
     28 
     29 void ConvertUInt64ToString(UInt64 value, wchar_t *s)
     30 {
     31   wchar_t temp[32];
     32   int pos = 0;
     33   do
     34   {
     35     temp[pos++] = (wchar_t)(L'0' + (int)(value % 10));
     36     value /= 10;
     37   }
     38   while (value != 0);
     39   do
     40     *s++ = temp[--pos];
     41   while (pos > 0);
     42   *s = L'\0';
     43 }
     44 
     45 void ConvertUInt32ToString(UInt32 value, char *s) { ConvertUInt64ToString(value, s); }
     46 void ConvertUInt32ToString(UInt32 value, wchar_t *s) { ConvertUInt64ToString(value, s); }
     47 
     48 void ConvertInt64ToString(Int64 value, char *s)
     49 {
     50   if (value < 0)
     51   {
     52     *s++ = '-';
     53     value = -value;
     54   }
     55   ConvertUInt64ToString(value, s);
     56 }
     57 
     58 void ConvertInt64ToString(Int64 value, wchar_t *s)
     59 {
     60   if (value < 0)
     61   {
     62     *s++ = L'-';
     63     value = -value;
     64   }
     65   ConvertUInt64ToString(value, s);
     66 }
     67 
     68 void ConvertUInt32ToHexWithZeros(UInt32 value, char *s)
     69 {
     70   for (int i = 0; i < 8; i++)
     71   {
     72     int t = value & 0xF;
     73     value >>= 4;
     74     s[7 - i] = (char)((t < 10) ? ('0' + t) : ('A' + (t - 10)));
     75   }
     76   s[8] = '\0';
     77 }
     78