int itoa(long num,int str,char radix)
//integer to array
//把num转化成相对应的字符串,把结果放在str指向的字符串中,输出字符串的进制取决于radix,它的值可以是2到36的任何值
{
char i,ch,neg;
neg=0;
if(num<0){num=abs(num);neg=1;}
else if(num==0)strcpy(str,"0");
while(num) {
i=num%radix;
num=num/radix;
if(i>=0&&i<=9)ch=i|0x30;
if(i>9&&i<36)ch=i+55;
memmove(str+1,str,strlen(str)+1);
*str=ch;
}
if(neg)
{
memmove(str+1,str,strlen(str)+1);
*str='-';
}
return str;
}
int atoi(int str,char radix)
//array to integer
//把str指向的字符串转化为一个整数,radix为进制.取值为2-36;
{
long num;
int i,sign;
char ch;
num=0;
ch=0xff;
if(*str=='-'){sign=-1;i=1;} else {sign=1;i=0;}
for(;ch;i++)
{
ch=*(str+i);
if(isdigit(ch))num=(num*radix)+(ch&0xf); else if(islower(ch))num=(num*radix)+(ch-87);
else if(isupper(ch))num=(num*radix)+(ch-55);
}
return sign*num;
}
void main()
{
char string[20];
printf(itoa(-78,string,10));
printf("\n%d",atoi("-7788",16));
}
本文地址:http://com.8s8s.com/it/it25806.htm