home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff254.lzh / Etale / ltoa.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  900b  |  39 lines

  1. /*   ltoa.c  -- not quite standard long-to-string, any base <= 16    */
  2. #ifndef EXEC_TYPES_H
  3. #include "exec/types.h"
  4. #endif
  5. SHORT ltoa(str, val, base)    /*  convert long int 'val' to string 'str'  */
  6. UBYTE *str;                   /*  returns SHORT = strlen(str)             */
  7. LONG val;
  8. SHORT base;
  9. {
  10. static char numchar[17] = "0123456789ABCDEF";
  11. static char tstr[20] = "";
  12. LONG div, lbase, rem;
  13. SHORT ilt, jlt, len;
  14. if (base < 2 || base > 16) lbase = 10L;
  15. else lbase = (long)base;
  16. div = 0;
  17. div = (val >=0L)? val : -val;
  18. ilt = 0;
  19. if(div == 0L) {tstr[0] = numchar[0]; ilt ++;}
  20. while (div > 0L)
  21.      {
  22.      rem = div%lbase;
  23.      tstr[ilt] = numchar[rem];
  24.      div = div/lbase;
  25.      ilt ++;
  26.      }
  27. if (val < 0L) { tstr[ilt] = '-'; ilt ++; }
  28. tstr[ilt] = '\0';
  29. len = ilt;
  30. jlt = len -1;
  31. for (ilt = 0; ilt < len; ilt ++)
  32.      {
  33.      str[ilt] = tstr[jlt];
  34.      jlt --;
  35.      }
  36. str[len] = '\0';
  37. return len;
  38. }
  39.