atoi(),atof(),atol();iato(),fato(),lato()---字符串和数字互相转换

来源:互联网 发布:但丁捏脸数据 编辑:程序博客网 时间:2024/05/16 09:10

1.double atof(const char *nptr);
把字符串转换成浮点数值。
<math.h>或<stdlib.h>
nptr:带转换的字符串。
若无溢出,返回字符串的双精度浮点数值。


int atoi(const char* nptr);
把字符串转换成整型数。
<stdlib.h>
nptr:带转换的字符串。
成功转换,返回字符串的整数值;若无法转换返回0.


long atol(const char* nptr);
将字符串转换成长整形数值。
<stdlib.h>
nptr: 待转换的字符串。
若无溢出,返回字符串的长整形数值。


2.数字转化为字符串

  ● itoa():将整型值转换为字符串。
  ● ltoa():将长整型值转换为字符串。
  ● ultoa():将无符号长整型值转换为字符串。
  ● gcvt():将浮点型数转换为字符串,取四舍五入。
  ● ecvt():将双精度浮点型值转换为字符串,转换结果中不包含十进制小数点。
  ● fcvt():指定位数为转换精度,其余同ecvt()。

还可以使用sprintf系列函数把数字转换成字符串,其比itoa()系列函数运行速度慢。下列程序演示了如何使用itoa()函数和gcvt()函数:

# include <stdio.h># include <stdlib.h>int main (){  int num_int = 435;    double num_double = 435.10f;    char str_int[30];    char str_double[30];    itoa(num_int, str_int, 10);  //把整数num_int转成字符串str_int   参数 10表示按十进制类型进行转换    gcvt(num_double, 8, str_double);  //把浮点数num_double转成字符串str_double   参数8表示精确位数    printf("str_int: %s\n", str_int);    printf("str_double: %s\n", str_double);   return 0;}
结果:
str_int: 435
str_double: 435.10001

如果不使用atoi或sprintf等库函数,可以通过把整数的各位上的数字加“0”转换成char类型并存到字符数组中。但是要注意,需要采用字符串逆序的方法。如以下程序所示:

1    #include <iostream>2    using namespace std;3   4    void int2str(int n, char *str)5    {6        char buf[10] = "";7        int i = 0;8        int len = 0;9        int temp = n < 0 ? -n: n;  // temp为n的绝对值10  11       if (str == NULL)12       {13           return;14       }15       while(temp)16       {17           buf[i++] = (temp % 10) + '0';  //把temp的每一位上的数存入buf18           temp = temp / 10;19       }20  21       len = n < 0 ? ++i: i;  //如果n是负数,则多需要一位来存储负号22       str[i] = 0;            //末尾是结束符023       while(1)24       {25           i--;26           if (buf[len-i-1] ==0)27           {28               break;29           }30           str[i] = buf[len-i-1];  //把buf数组里的字符拷到字符串31       }32       if (i == 0 )33       {34           str[i] = '-';          //如果是负数,添加一个负号35       }36   }37  38   int main()39   {40       int nNum;41       char p[10];42  43       cout << "Please input an integer:";44       cin >> nNum;45       cout << "output: " ;46       int2str(nNum, p);        //整型转换成字符串47       cout<< p << endl;48  49       return 0;50   }
结果:

程序中的int2str函数完成了int类型到字符串类型的转换。在代码第46行对int2str函数做了测试。程序的执行结果如下所示:
Please input an integer: 1234
Output: 1234
如果输入的是个负数,程序执行结果如下所示:
Please input an integer: -1234
Output: -1234

3. 字符串转化为数字

  ● atof():将字符串转换为双精度浮点型值。
  ● atoi():将字符串转换为整型值。
  ● atol():将字符串转换为长整型值。
  ● strtod():将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字。
  ● strtol():将字符串转换为长整值,并报告不能被转换的所有剩余数字。
  ● strtoul():将字符串转换为无符号长整型值,并报告不能被转换的所有剩余数字。

以下程序演示如何使用atoi ()函数和atof ()函数:

1    # include <stdio.h>2    # include <stdlib.h>3   4    int main ()5    {6        int num_int;7        double num_double;8        char str_int[30] = "435";         //将要被转换为整型的字符串9        char str_double[30] = "436.55";  //将要被转换为浮点型的字符串10  11       num_int = atoi(str_int);          //转换为整型值12       num_double = atof(str_double);  //转换为浮点型值13  14       printf("num_int: %d\n", num_int);15       printf("num_double: %lf\n", num_double);16  17       return 0;18   }
结果:
num_int: 435
num_double: 436.550000

不使用库函数将字符串转换为数字:

1    #include <iostream>2    using namespace std;3   4    int str2int(const char *str)5    {6        int temp = 0;7        const char *ptr = str;  //ptr保存str字符串开头8   9        if (*str == '-' || *str == '+')  //如果第一个字符是正负号,10       {                      //则移到下一个字符11           str++;12       }13       while(*str != 0)14       {15           if ((*str < '0') || (*str > '9'))  //如果当前字符不是数字16           {                       //则退出循环17               break;18           }19           temp = temp * 10 + (*str - '0'); //如果当前字符是数字则计算数值20           str++;      //移到下一个字符21       }   22       if (*ptr == '-')     //如果字符串是以“-”开头,则转换成其相反数23       {24           temp = -temp;25       }26  27       return temp;28   }29  30   int main()31   {32       int n = 0;   33       char p[10] = "";34  35       cin.getline(p, 20);   //从终端获取一个字符串36       n = str2int(p);      //把字符串转换成整型数37       38       cout << n << endl;39  40       return 0;41   }
程序执行结果:
输入:1234
输出:1234
输入:-1234
输出:-1234
输入:+1234
输出:1234


参考:http://blog.sina.com.cn/s/blog_4c8a2a870100qgq7.html





0 0
原创粉丝点击