代码
//
int to ascii 整形转换成字符串 负数未实现
char * itoa( int num, char * str, int radix)
{
// 0的情况
if (num == 0 )
{
str[ 0 ] = " 0 " ;
str[ 1 ] = " \0 " ;
return str;
}
char string [] = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
char * ptr = str;
int i;
int j;
while (num)
{
* ptr ++ = string [num % radix];
num /= radix;
if (num < radix)
{
* ptr ++ = string [num];
* ptr = ' \0 ' ;
break ;
}
}
// 两边对调
j = ptr - str - 1 ;
for (i = 0 ; i < (ptr - str) / 2 ; i ++ )
{
int temp = str[i];
str[i] = str[j];
str[j -- ] = temp;
}
return str;
}
char * itoa( int num, char * str, int radix)
{
// 0的情况
if (num == 0 )
{
str[ 0 ] = " 0 " ;
str[ 1 ] = " \0 " ;
return str;
}
char string [] = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
char * ptr = str;
int i;
int j;
while (num)
{
* ptr ++ = string [num % radix];
num /= radix;
if (num < radix)
{
* ptr ++ = string [num];
* ptr = ' \0 ' ;
break ;
}
}
// 两边对调
j = ptr - str - 1 ;
for (i = 0 ; i < (ptr - str) / 2 ; i ++ )
{
int temp = str[i];
str[i] = str[j];
str[j -- ] = temp;
}
return str;
}