人生如逆旅,我亦是行人!江湖人称wsjjeremy.blog.chinaunix.net
ubuntuer
全部博文(930)
intern(3)
string(19)
正则表达式(5)
2011年(60)
2010年(220)
2009年(371)
2008年(279)
baocheng
nba76ers
renjian2
qq576709
mcn304
zibuyule
西农魔峰
曾德标
zhuqing_
shanck
tendy
moshangx
wb123456
smile124
hjshajsh
chenhong
bzhao
python16
分类: C/C++
2009-06-28 13:25:46
[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; }
上一篇:计算一个数bit位为1的总数目
下一篇:十道题测试下你的c语言功底
chinaunix网友2009-10-20 00:22:52
讨厌没有一丁点儿说明~~~
登录 注册