虽然C++种提供了to_string函数,但在数字过长的极端情况下to_string会出现问题,为此我写个转换函数放着里便于以后取用:
#include <iostream>
using namespace std;
char txt[100];
void to_characters(long long Num) // 将long long转换成char数组
{
memset(txt, '\0', 100);
char temporary[100];
int length = 0;
while (Num)
{
temporary[length++] = (Num % 10) + '0';
Num /= 10;
}
int j = 0;
for (int i = (length - 1); i >= 0; i--)
txt[j++] = temporary[i];
}
int main()
{
long long Num = 12345678987654;
to_characters(Num);
cout << txt << endl;
}