home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / smiley40.zip / LTOA.C < prev    next >
Text File  |  1993-01-17  |  2KB  |  96 lines

  1. /*
  2.  * ltoa(num, base) - return string form of the given number in the
  3.  * given base.
  4.  * 
  5.  * If the base has magnitude greater than 10, the sign of the base
  6.  * determines which case of letters will be used to represent digits
  7.  * larger than 9.  A positive base will use upper-case letters, while
  8.  * a negative base will use lower-case letters.
  9.  * 
  10.  * If the base has magnitude 10, the sign of the base determines whether
  11.  * the number is to be considered signed or unsigned.  A base of +10
  12.  * is considered to be signed, while a base of -10 is considered to
  13.  * be unsigned.  Odd, huh?
  14.  * 
  15.  * Only base 10 is potentially signed; all other bases are unsigned.
  16.  * 
  17.  * For performance reasons there is no check for an invalid base (a base
  18.  * of magnitude less than 1 or greater than 36).
  19.  * 
  20.  * The return value is a pointer to a static buffer that is overwritten
  21.  * with every call.
  22.  *
  23.  * DaviD W. Sanderson
  24.  */
  25.  
  26. char           *
  27. ltoa(num, obase)
  28.     unsigned long   num;
  29.     int             obase;
  30. {
  31.     unsigned long   base;
  32.     int             neg = 0;/* 1 if num is negative */
  33.     char            a = 'A';
  34.  
  35.     /*
  36.      * buf[] is big enough for the longest binary string together
  37.      * with terminal NUL and possible minus sign.
  38.      * 
  39.      * Note that there does not need to be a separate byte allocated
  40.      * for the minus sign. The minus sign is present only for
  41.      * numbers to be represented in decimal. Since the base 10
  42.      * representation of numbers larger than 1 is shorter than
  43.      * the base 2 representation, there will always be room for
  44.      * the minus sign in the base 10 string.
  45.      */
  46.     static char     buf[sizeof(unsigned long) * 8 + 1];
  47.  
  48.     /* p points to the last char in buf */
  49.     char           *p = buf + (sizeof buf - 1);
  50.  
  51.     *p = '\0';        /* terminate the string */
  52.  
  53.     /*
  54.      * The only signed-ness occurs when obase == +10.
  55.      * 
  56.      * If obase == -10, the decimal number is unsigned.
  57.      */
  58.     if (obase == 10 && (long) num < 0)
  59.     {
  60.         neg = 1;
  61.         num = -(long) num;
  62.     }
  63.  
  64.     if (obase < 0)
  65.     {
  66.         a = 'a';
  67.         obase = -obase;
  68.     }
  69.  
  70.     /*
  71.      * now that obase is nonnegative, assign it to base
  72.      */
  73.     base = (unsigned long) obase;
  74.  
  75.     /*
  76.      * subtract 10 from the code for the letter 'a' since rem
  77.      * will be 10 more than the offset from a
  78.      */
  79.     a -= 10;
  80.  
  81.     do
  82.     {
  83.         long            rem;
  84.  
  85.         rem = num % base;    /* obtain value of digit */
  86.  
  87.         *--p = ((rem < 10) ? '0' : a) + rem;
  88.     }
  89.     while ((num /= base) != 0);
  90.  
  91.     if (neg)
  92.         *--p = '-';
  93.  
  94.     return p;
  95. }
  96.