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