home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libcnews / ltoza.c < prev    next >
C/C++ Source or Header  |  1990-02-10  |  1KB  |  54 lines

  1. /*
  2.  * ltoza, ltozan - long to zero-padded ascii conversions
  3.  *
  4.  * These functions exist only because there is no portable way
  5.  * to do this with printf and there may be no way do it at all
  6.  * with printf on V7, due to a bug in V7's printf.
  7.  */
  8.  
  9. #define RADIX 10
  10.  
  11. /*
  12.  * convert value to at most width characters in outstr, padding with
  13.  * zeros on the left (after any sign); do not terminate with a NUL.
  14.  * returns true iff the value fits in width characters.
  15.  */
  16. int                    /* boolean */
  17. ltozan(outstr, value, width)
  18. char *outstr;
  19. long value;
  20. int width;
  21. {
  22.     register char *op = outstr;
  23.     register long wval = value;
  24.     register int wwid = width;
  25.  
  26.     if (wval < 0 && wwid > 0) {
  27.         *op++ = '-';
  28.         --wwid;
  29.         wval = -wval;        /* fails on smallest int; tough */
  30.     }
  31.     op += wwid - 1;            /* find right end */
  32.     while (wwid-- > 0) {        /* generate "wwid" digits */
  33.         *op-- = wval % RADIX + '0';
  34.         wval /= RADIX;
  35.     }
  36.     return wval == 0;
  37. }
  38.  
  39. /*
  40.  * convert value to at most width characters in outstr, padding with
  41.  * zeros on the left (after any sign); terminate with a NUL.
  42.  */
  43. int                    /* boolean */
  44. ltoza(outstr, value, width)
  45. register char *outstr;            /* char outstr[width+1]; */
  46. long value;
  47. register int width;
  48. {
  49.     register int fits = ltozan(outstr, value, width);
  50.  
  51.     outstr[width] = '\0';
  52.     return fits;
  53. }
  54.