Advertisement
tyotyotyo

dectohex

Apr 22nd, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. const char __stdlib_int_to_hex_table[16] =
  2. {
  3.     '0', '1', '2', '3','4','5','6','7','8','9','a','b','c','d','e','f',
  4. };
  5.  
  6. // lower case
  7. // size of `buf` must be enough for 'ffffffffffffffff' so buf[16]
  8. char __CRT_itoa_internalBuffer[30]; // need to fix itoa(123) = "321"
  9. void
  10. __CRT_dectohex(uint64_t dec, char* buf, int* sz)
  11. {
  12.     if (dec)
  13.     {
  14.         double decInDouble = (double)dec;
  15.         int bufInd = 0;
  16.         while (1)
  17.         {
  18.             decInDouble /= 16.0;
  19.  
  20.             double ipart = 0.0;
  21.             int i = (int)(modf(decInDouble, &ipart) * 16.0);//(dec % 16) * 16;
  22.             dec /= 16; // for exit from loop
  23.  
  24.             __CRT_itoa_internalBuffer[bufInd++] = __stdlib_int_to_hex_table[i];
  25.  
  26.             if (!dec)
  27.                 break;
  28.         }
  29.         *sz = bufInd;
  30.         if (bufInd)
  31.         {
  32.             for (int i = 0; i < bufInd; ++i)
  33.             {
  34.                 buf[i] = __CRT_itoa_internalBuffer[bufInd - i - 1];
  35.             }
  36.             buf[bufInd] = 0;
  37.         }
  38.     }
  39.     else
  40.     {
  41.         buf[0] = '0';
  42.         buf[1] = 0;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement