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';//+'0'目的将整数转化为字符
temp = temp / 10;
}
len = n < 0 ? ++i:i;//n是负数,则多需一位来存储负号
str[i] = 0;//末尾是结束符0
while (1)
{
i--;
if (buf[len-i-1]==0)
{
break;
}
str[i] = buf[len-i-1];//把buf数组里的字符拷到字符串
}
if (i==0)
{
str[i] = '-';
}
}

892

被折叠的 条评论
为什么被折叠?



