#include<iostream> //整型转化为字符串
using namespace std;
void int2str( int n, char *str)
{
char buf[10] = "";
int i = 0;
int len = 0;
int temp = n<0 ? -n : n;
if( str==NULL )
{
return;
}
while(temp)
{
buf[i++]=(temp%10)+'0';
temp=temp/10;
}
len = n<0?++i:i; //关键所在
str[i]=0;
while(1)
{
i--;
if(buf[len-i-1]==0)
{
break;
}
str[i] = buf[len-i-1];
}
if( i==0 ) //正数是i=-1
{
str[i]='-';
}
}
int main(int argc, char* argv[])
{
int num=-123;
char str[10];
int2str(num, str);
cout<<str<<endl;
return 0;
}
整型转化为字符串(相互)
最新推荐文章于 2020-12-19 23:58:22 发布