ltostr.c 906 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (C) 2000 Manuel Novoa III
  3. *
  4. * Note: buf is a pointer to the END of the buffer passed.
  5. * Call like this:
  6. * char buf[SIZE], *p;
  7. * p = __ltostr(buf + sizeof(buf) - 1, ...)
  8. *
  9. * For longs of 32 bits, appropriate buffer sizes are:
  10. * base = 2 34 = 1 (possible -) sign + 32 digits + 1 nul
  11. * base = 10 12 = 1 (possible -) sign + 10 digits + 1 nul
  12. * base = 16 10 = 1 (possible -) sign + 8 hex digits + 1 nul
  13. */
  14. extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
  15. char *__ltostr(char *buf, long val, int base, int uppercase)
  16. {
  17. unsigned long uval;
  18. char *pos;
  19. int negative;
  20. negative = 0;
  21. if (val < 0) {
  22. negative = 1;
  23. uval = ((unsigned long)(-(1+val))) + 1;
  24. } else {
  25. uval = val;
  26. }
  27. pos = __ultostr(buf, uval, base, uppercase);
  28. if (pos && negative) {
  29. *--pos = '-';
  30. }
  31. return pos;
  32. }