#include<stdio.h>
char *itoa(int val, char *buf, unsigned radix)
{
char *p;
char *firstdig;
char temp;
unsigned digval;
p = buf;
if(val <0)
{
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p;
do{
digval = (unsigned)(val % radix);
val /= radix;
if(digval > 9)
*p++ = (char)(digval - 10 + 'a');
else
*p++ = (char)(digval + '0');
}while(val > 0);
*p-- = '\0';
// 字符串逆序
do{
temp = *p;
*p = *firstdig;
*firstdig = temp;
--p;
++firstdig;
}while(firstdig < p);
return buf;
}
main()
{
char c[20];
}
char *itoa(int val, char *buf, unsigned radix)
{
char *p;
char *firstdig;
char temp;
unsigned digval;
p = buf;
if(val <0)
{
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p;
do{
digval = (unsigned)(val % radix);
val /= radix;
if(digval > 9)
*p++ = (char)(digval - 10 + 'a');
else
*p++ = (char)(digval + '0');
}while(val > 0);
*p-- = '\0';
// 字符串逆序
do{
temp = *p;
*p = *firstdig;
*firstdig = temp;
--p;
++firstdig;
}while(firstdig < p);
return buf;
}
main()
{
char c[20];
printf("%s",itoa(-123,c,10));
//printf("%s",itoa(-123,c,16));
}