atoi() 与 itoa()函数的用法

来源:互联网 发布:spss 23 mac 许可证 编辑:程序博客网 时间:2024/06/04 06:44


C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转 换为字符串的一个例子:

       atoi     把字符串转换成整型数
       itoa     把一整数转换为字符串
 

[cpp] view plaincopy
  1. #include "stdio.h"  
  2. #include "ctype.h"  
  3. #include "stdlib.h"  
  4.   
  5. /* 
  6. Converts a character string into an int or long 
  7. 将一个字符串转化为整数 
  8. */  
  9. int my_atoi(char s[])  
  10. {  
  11.     int i,n,sign;  
  12.   
  13.     for(i=0;isspace(s[i]);i++);   //跳过空白  
  14.   
  15.     sign=(s[i]=='-')?-1:1;  
  16.     if(s[i]=='+'||s[i]==' -')     //跳过符号位  
  17.         i++;  
  18.     for(n=0;isdigit(s[i]);i++)  
  19.         n=10*n+(s[i]-'0');        //将数字字符转换成整形数字  
  20.     return sign*n;  
  21. }  
  22.   
  23. /* 
  24. Converts an int or long into a character string 
  25. 将一个整数转化为字符串 
  26. */  
  27. void my_itoa(int n,char s[])  
  28. {  
  29.     int i,j,sign;  
  30.   
  31.     if((sign=n)<0)    //记录符号  
  32.         n=-n;         //使n成为正数  
  33.     i=0;  
  34.     do{  
  35.         s[i++]=n%10+'0';    //取下一个数字  
  36.     }while((n/=10)>0);      //循环相除  
  37.   
  38.     if(sign<0)  
  39.         s[i++]='-';  
  40.     s[i]='\0';  
  41.     for(j=i-1;j>=0;j--)        //生成的数字是逆序的,所以要逆序输出  
  42.         printf("%c",s[j]);  
  43. }  
  44.   
  45.   
  46. void main()  
  47. {  
  48.     int n;  
  49.     char str[100];  
  50.     my_itoa(-123,str);  
  51.     printf("\n");  
  52.     printf("%d\n",my_atoi("123"));  
  53.     system("pause");  

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进制表示的字符串。

下面是一个十进制转八进制的方法:

[cpp] view plaincopy
  1. #include "stdio.h"  
  2. #include "stdlib.h"  
  3.   
  4. int main(void)  
  5. {  
  6.     int num = 10;  
  7.     char str[100];  
  8.     itoa(num, str, 8);      //将整数10转换为八进制保存在str字符数组中  
  9.     printf("%s\n", str);  
  10.     system("pause");  
  11.     return 0;  
  12. }  

下面是一个十进制转二进制的方法:

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

itoa()函数的扩展:

char *_itoa( int value, char *string, int radix );

char *_i64toa( __int64 value, char *string, int radix );

char * _ui64toa( unsigned _int64 value, char *string, int radix );

wchar_t * _itow( int value, wchar_t *string, int radix );

wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );

wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );

 程序代码如下:

[cpp] view plaincopy
  1. #include "stdio.h"  
  2. #include "stdlib.h"  
  3.   
  4. int main(void)  
  5. {  
  6.     char buffer[20];  
  7.     int i = 3445;  
  8.     long l = -344115L;  
  9.     unsigned long ul = 1234567890UL;  
  10.   
  11.     _itoa( i, buffer, 10 );  
  12.     printf( "String of integer %d (radix 10): %s\n", i, buffer );  
  13.     _itoa( i, buffer, 16 );  
  14.     printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );  
  15.     _itoa( i, buffer, 2 );  
  16.     printf( "String of integer %d (radix 2): %s\n", i, buffer );  
  17.   
  18.     _ltoa( l, buffer, 16 );  
  19.     printf( "String of long int %ld (radix 16): 0x%s\n", l,buffer );  
  20.   
  21.     _ultoa( ul, buffer, 16 );  
  22.     printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,buffer );  
  23.   
  24.     system("pause");  
  25.     return 0;  

http://blog.csdn.net/hackbuteer1/article/details/6666959
原创粉丝点击