首页 > 其他 > 详细

atoi和itoa

时间:2015-06-14 16:32:41      阅读:203      评论:0      收藏:0      [点我收藏+]
 itoa()函数的原型为: char *itoa( int value, char *string,int radix);itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转换数字时所用的基数。在例中,转换基数为10。10:十进制;2:二进制...itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。 
是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:char str[255]; 
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。下面是一个十进制转八进制的方法:

 1 #include "stdio.h"  
 2 
 3 #include "stdlib.h"  
 4 
 5   
 6 
 7 int main(void)  
 8 
 9 {  
10 
11     int num = 10;  
12 
13     char str[100];  
14 
15     itoa(num, str, 8);      //将整数10转换为八进制保存在str字符数组中  
16 
17     printf("%s\n", str);  
18 
19     system("pause");  
20 
21     return 0;  
22 
23 }  
 
 
下面是一个十进制转二进制的方法:

技术分享
 1 #include "stdio.h"  
 2 
 3 #include "stdlib.h"  
 4 
 5   
 6 
 7 int main(void)  
 8 
 9 {  
10 
11     int num = 15;  
12 
13     char str[100];  
14 
15     int n = atoi(itoa(num, str, 2));   //先把num转换为二进制的字符串,再把该字符串转换为整数  
16 
17     printf("%d\n",n);  
18 
19     system("pause");  
20 
21     return 0;  
22 }

 



atoi和itoa

原文:http://www.cnblogs.com/wangmengmeng/p/4575148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!