lltostr.c 940 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 = __lltostr(buf + sizeof(buf) - 1, ...)
  8. * For long longs of 64 bits, appropriate buffer sizes are:
  9. * base = 2 66 = 1 (possible -) sign + 64 digits + 1 nul
  10. * base = 10 21 = 1 (possible -) sign + 19 digits + 1 nul
  11. * base = 16 18 = 1 (possible -) sign + 16 hex digits + 1 nul
  12. */
  13. extern char *__ulltostr(char *buf, unsigned long long uval, int base,
  14. int uppercase);
  15. char *__lltostr(char *buf, long long val, int base, int uppercase)
  16. {
  17. unsigned long long uval;
  18. char *pos;
  19. int negative;
  20. negative = 0;
  21. if (val < 0) {
  22. negative = 1;
  23. uval = ((unsigned long long)(-(1+val))) + 1;
  24. } else {
  25. uval = val;
  26. }
  27. pos = __ulltostr(buf, uval, base, uppercase);
  28. if (pos && negative) {
  29. *--pos = '-';
  30. }
  31. return pos;
  32. }