ltostr.c 819 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. static char buf[34];
  6. extern char *ultostr();
  7. char *ltostr(val, radix, uppercase)
  8. long val;
  9. int radix;
  10. int uppercase;
  11. {
  12. char *p;
  13. int flg = 0;
  14. if (val < 0) {
  15. flg++;
  16. val = -val;
  17. }
  18. p = ultostr(val, radix, uppercase);
  19. if (p && flg)
  20. *--p = '-';
  21. return p;
  22. }
  23. char *ultostr(val, radix, uppercase)
  24. unsigned long val;
  25. int radix;
  26. int uppercase;
  27. {
  28. register char *p;
  29. register int c;
  30. if (radix > 36 || radix < 2)
  31. return 0;
  32. p = buf + sizeof(buf);
  33. *--p = '\0';
  34. do {
  35. c = val % radix;
  36. val /= radix;
  37. if (c > 9)
  38. *--p = (uppercase ? 'A' : 'a') - 10 + c;
  39. else
  40. *--p = '0' + c;
  41. }
  42. while (val);
  43. return p;
  44. }