home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / jplc2 / itoa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-22  |  2.1 KB  |  77 lines

  1. /* 1.1  01-08-86                        (itoa.c)
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1986        *
  6.  ************************************************************************
  7.  *
  8.  *    These functions are modified versions of itoa() found in Kernighan
  9.  *    and Ritchie, page 60, with modifications as suggested in exercises
  10.  *    3-3 and 3-5, and with returned pointer to result string.
  11.  *
  12.  *----------------------------------------------------------------------*/
  13.  
  14. #include "defs.h"
  15. #include "stdtyp.h"
  16. #include "errno.h"
  17.  
  18. /************************************************************************/
  19.     STRING
  20. itoa(s, n, w)    /* return pointer to s, which contains ascii value of
  21.            n to width |w|, 0-filled if w<0, else space-filled.    */
  22. /*----------------------------------------------------------------------*/
  23. STRING s;
  24. {
  25.     STRING itoab();
  26.  
  27.     return itoab(s, n, w, 10);
  28. }
  29.  
  30. /************************************************************************/
  31.     STRING
  32. itoab(s, n, w, b)    /* convert n to a minimum of |w| ascii characters,
  33.                base b, and put  in s.  Return pointer to s.
  34.                0-fill if w<0, else space-fill.        */
  35. /*----------------------------------------------------------------------*/
  36. STRING s;
  37. {
  38.     LOCAL char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  39.     int i, j;
  40.     BOOL sign;
  41.     char c, fill;
  42.  
  43.     if (n < 0)
  44.     {    n = -n;
  45.         sign = TRUE;
  46.     }
  47.     else
  48.         sign = FALSE;
  49.     if (w < 0)
  50.     {    fill = '0';
  51.         w = -w;
  52.     }
  53.     else
  54.         fill = ' ';
  55.     i = 0;
  56.     do            /* generate digits in reverse order:    */
  57.     {    s[i++] = digits[n % b];
  58.     } while (n /= b);
  59.     if (sign)            /* put the sign into the string.*/
  60.         s[j = i++] = '-';
  61.     else
  62.         j = i - 1;
  63.     while (i < w)            /* expand to proper width.    */
  64.         s[i++] = fill;    /* i is just beyond last filled postion    */
  65.     if (--i ISNT j AND sign AND fill IS '0')
  66.     {    s[i] = s[j];        /* get the sign.        */
  67.         s[j] = fill;        /* fill in over it        */
  68.     }
  69.     s[++i] = NULL;            /* add null terminator    */
  70.     for (i--, j = 0; j < i; j++, i--) /*  now reverse chars */
  71.     {    c = s[j];
  72.         s[j] = s[i];
  73.         s[i] = c;
  74.     }
  75.     return s;
  76. }
  77.