stdlib.c 587 B

1234567891011121314151617181920212223242526272829
  1. #include <common/stdlib.h>
  2. /**
  3. * @brief 将长整型转换为字符串
  4. *
  5. * @param input 输入的数据
  6. * @return const char* 结果字符串
  7. */
  8. const char *ltoa(long input)
  9. {
  10. /* large enough for -9223372036854775808 */
  11. static char buffer[21] = {0};
  12. char *pos = buffer + sizeof(buffer) - 1;
  13. int neg = input < 0;
  14. unsigned long n = neg ? -input : input;
  15. *pos-- = '\0';
  16. do
  17. {
  18. *pos-- = '0' + n % 10;
  19. n /= 10;
  20. if (pos < buffer)
  21. return pos + 1;
  22. } while (n);
  23. if (neg)
  24. *pos-- = '-';
  25. return pos + 1;
  26. }