分类: LINUX
2010-07-30 15:26:23
[root@localhost bishi]# cat itoa.c
#include <stdio.h>
#include <stdlib.h>
char* itoa(int value, char* str, int radix)
{
static char dig[] ="0123456789"
"abcdefghijklmnopqrstuvwxyz";
int n = 0, neg = 0;
unsigned int v;
char* p;
char* q;
char c;
if (radix == 10 && value < 0) {
value = -value;
neg = 1;
}
v = value;
do {
str[n++] = dig[v%radix];
v /= radix;
} while (v);
if (neg)
str[n++] = '-';
str[n] = '\0';
for (p = str, q = p+n-1; (p<q)&&(*q!='\0'); ++p, --q)
{
c = *p;
*p = *q;
*q = c;
}
return str;
}
int main(int argc,char** argv)
{
char str[32];
if(argc != 3)
{
printf("Usage:%s num radix\n",argv[0]);
return -1;
}
int i = atoi(argv[1]);
int j = atoi(argv[2]);
if(j>16)
{
printf("radix error\n");
return -1;
}
itoa(i,str,j);
printf("num:%d radix:%d str is %s\n",i,j,str);
return 0;
}