Advertisement
Guest User

Untitled

a guest
May 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. static void
  2.      itoa (char *buf, int base, int d)
  3.      {
  4.        char *p = buf;
  5.        char *p1, *p2;
  6.        unsigned long ud = d;
  7.        int divisor = 10;
  8.      
  9.        /* If %d is specified and D is minus, put `-' in the head. */
  10.        if (base == 'd' && d < 0)
  11.          {
  12.            *p++ = '-';
  13.            buf++;
  14.            ud = -d;
  15.          }
  16.        else if (base == 'x')
  17.          divisor = 16;
  18.      
  19.        /* Divide UD by DIVISOR until UD == 0. */
  20.        do
  21.          {
  22.            int remainder = ud % divisor;
  23.      
  24.            *p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
  25.          }
  26.        while (ud /= divisor);
  27.      
  28.        /* Terminate BUF. */
  29.        *p = 0;
  30.      
  31.        /* Reverse BUF. */
  32.        p1 = buf;
  33.        p2 = p - 1;
  34.        while (p1 < p2)
  35.          {
  36.            char tmp = *p1;
  37.            *p1 = *p2;
  38.            *p2 = tmp;
  39.            p1++;
  40.            p2--;
  41.          }
  42.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement