home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LTOSTR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  97 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  LTOSTR.C - An improved, safer, ltoa()
  5. **
  6. **  On call:
  7. **  num       =   number to convert
  8. **  string    =   buffer for output
  9. **  max_chars =   maximum size of buffer
  10. **  base      =   number base for conversion.
  11. **
  12. **  Return value:
  13. **  if illegal base
  14. **    NULL
  15. **  beginning of converted number.
  16. **
  17. **  notes: if number is too large in magnitude to fit in the buffer,
  18. **  the MOST significant digits will be truncated.  If the number is
  19. **  negative, a leading '-' will be placed in the buffer even if this
  20. **  causes the most significant digit to be truncated.
  21. **  The number is right justified in the buffer and the location of the
  22. **  first character in the number is returned so:
  23. **    If you want right justification, use the original string.
  24. **    If you want left justification, use the returned string.
  25. **  If the number doesn't fill the buffer:
  26. **       leading characters will be filled with spaces.
  27. **
  28. **  public domain by Jerry Coffin
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "numcnvrt.h"
  34.  
  35. char *ltostr(long num, char *string, size_t max_chars, unsigned base)
  36. {
  37.       char remainder;
  38.       int sign = 0;   /* number of digits occupied by the sign. */
  39.  
  40.       if (base < 2 || base > 36)
  41.             return NULL;
  42.  
  43.       if (num < 0)
  44.       {
  45.             sign = 1;
  46.             num = -num;
  47.       }
  48.  
  49.       string[--max_chars] = '\0';
  50.  
  51.       for (max_chars--; max_chars > sign && num!=0; max_chars --)
  52.       {
  53.             remainder = (char) (num % base);
  54.             if ( remainder < 9 )
  55.                   string[max_chars] = remainder + '0';
  56.             else  string[max_chars] = remainder - 10 + 'A';
  57.             num /= base;
  58.       }
  59.  
  60.       if (sign)
  61.             string[--max_chars] = '-';
  62.  
  63.       if ( max_chars > 0 )
  64.             memset(string, ' ', max_chars+1);
  65.  
  66.       return string + max_chars;
  67. }
  68.  
  69. #ifdef TEST
  70.  
  71. #include <stdlib.h>
  72.  
  73. #ifdef __WATCOMC__
  74.  #pragma off (unreferenced);
  75. #endif
  76.  
  77. #ifdef __TURBOC__
  78.  #pragma argsused
  79. #endif
  80.  
  81. #define SIZE 50
  82.  
  83. int main(int argc, char *argv[])
  84. {
  85.       char buffer[SIZE];
  86.  
  87.       long number   = atoi(argv[1]);
  88.       unsigned base = atoi(argv[2]);
  89.  
  90.       printf("%ld in base %u is \"%s\"\n", number, base,
  91.             ltostr(number, buffer, SIZE, base));
  92.  
  93.       return 0;
  94. }
  95.  
  96. #endif /* TEST */
  97.