itoa函数和atoi函数的用法

    技术2022-05-20  49

    itoa函数和atoi函数的用法 - 温海东的日志 - 网易博客

    MFC 2009-11-04 12:15:27 阅读174 评论0   字号: 订阅

    itoa函数和atoi函数的用法

    默认分类   2009-09-21 15:57

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

    # include <stdio.h>

    # include <stdlib.h>

    void main (void)

    {

    int num = 100;

    char str[25];

    itoa(num, str, 10);

    printf("The number 'num' is %d and the string 'str' is %s. /n" ,num, str);

    }

    itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。在上例中,转换基数为10。10:十进制;2:二进制... itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。

    是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:

    char str[255];

    sprintf(str, "%x", 100); //将100转为16进制表示的字符串。

    下列函数可以将整数转换为字符串:

    ----------------------------------------------------------

    函数名 作 用

    ----------------------------------------------------------

    itoa() 将整型值转换为字符串

    itoa() 将长整型值转换为字符串

    ultoa() 将无符号长整型值转换为字符串一  atoi     把字符串转换成整型数例程序:#include <ctype.h>

    #include <stdio.h>

    int atoi (char s[]);

    int main(void )

    {   char s[100];gets(s);printf("integer=%d/n",atoi(s));

    return 0;

    }

    int atoi (char s[])

    {

    int i,n,sign;for(i=0;isspace(s[i]);i++)//跳过空白符

           ;

    sign=(s[i]=='-')?-1:1;

    if(s[i]=='+'||s[i]==' -')//跳过符号

           i++;

    for(n=0;isdigit(s[i]);i++)

           n=10*n+(s[i]-'0');//将数字字符转换成整形数字

    return sign *n;}二        itoa      把一整数转换为字符串例程序:#include <ctype.h>

    #include <stdio.h>

    void       itoa (int n,char s[]);

    //atoi 函数:将s转换为整形数

    int main(void )

    {   

    int n;

    char s[100];printf("Input n:/n");

    scanf("%d",&n);         printf("the string : /n");

             itoa (n,s);

    return 0;

    }

    void itoa (int n,char s[])

    {

    int i,j,sign;if((sign=n)<0)//记录符号

           n=-n;//使n成为正数

             i=0;

    do{

           s[i++]=n+'0';//取下一个数字

    }while ((n/=10)>0);//删除该数字if(sign<0)

           s[i++]='-';

    s[i]='/0';

    for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出

           printf("%c",s[j]);

    }

    -------------------------------------------------------------- atoi, atol, atoll, atoq -- convert a string to an integer

    相关函数: atof, atol, atrtod, strtol, strtoul

    表头文件: #include <stdlib.h>

    定义函数: int atoi(const char *nptr);

    函数说明: atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('/0‘)才结束转换,并将结果返回。

    返回值:   返回转换后的整型数。

    附加说明: atoi(nptr)与strtol(nptr, (char**)NULL, 10)结果相同

    #include <stdlib.h>

    #include <stdio.h>

    main() {

        char     *a = "-100";

        char     *b = "456";

        int     c;

        c = atoi(a) + atoi(b); //356

        printf("c = %d/n", c);

    }

    **************************************************************************

    From:http://topic.csdn.net/t/20000908/13/29021.html

    itoa,下面是例子  

      #include   <stdlib.h>  

      #include   <stdio.h>  

       

      void   main(   void   )  

      {  

            char   buffer[20];  

            int     i   =   3445;  

            long   l   =   -344115L;  

            unsigned   long   ul   =   1234567890UL;  

       

            _itoa(   i,   buffer,   10   );  

            printf(   "String   of   integer   %d   (radix   10):   %s/n",   i,   buffer   );  

            _itoa(   i,   buffer,   16   );  

            printf(   "String   of   integer   %d   (radix   16):   0x%s/n",   i,   buffer   );  

            _itoa(   i,   buffer,   2     );  

            printf(   "String   of   integer   %d   (radix   2):   %s/n",   i,   buffer   );  

       

            _ltoa(   l,   buffer,   16   );  

            printf(   "String   of   long   int   %ld   (radix   16):   0x%s/n",   l,    

                                                                                                              buffer   );  

       

            _ultoa(   ul,   buffer,   16   );  

            printf(   "String   of   unsigned   long   %lu   (radix   16):   0x%s/n",   ul,  

                                                                                                              buffer   );  

      }  

       

       

      Output  

       

      String   of   integer   3445   (radix   10):   3445  

      String   of   integer   3445   (radix   16):   0xd75  

      String   of   integer   3445   (radix   2):   110101110101  

      String   of   long   int   -344115   (radix   16):   0xfffabfcd  

      String   of   unsigned   long   1234567890   (radix   16):   0x499602d2 


    最新回复(0)