几种c/c++中字符串转整形的方法

1.自己写一个函数(c/c++)

  1. #include <stdio.h>
  2. #include <assert.h>
  3. /*  my string to integer function  */
  4. int myfun(char *str){
  5.     int i = 0,n = 0,flag = 1;
  6.     if(str[0] == '-')
  7.         i = 1;flag = -1;
  8.     for(; str[i] != '/0' ; i++){
  9.         assert(str[i] >= '0' && str[i] <= '9');
  10.         n = str[i] - '0' + n*10;
  11.     }
  12.     return n*flag;
  13. }
  14. int main(int argc, char *argv[])
  15. {
  16.     int a;
  17.     char str[] = "1024";
  18.     a = myfun(str);
  19.     printf("%d/n",a);
  20.     return 0;
  21. }

2.使用c标准库中的atoi函数(c/c++)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.     int a;double d;
  6.     char str[] = "1024";
  7.     char strd[] = "3.1415";
  8.     a = atoi(str);d =atof(strd);
  9.     printf("%d/n",a);
  10.     printf("%g/n",d);
  11.     return 0;
  12. }
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(int argc, char *argv[])
  5. {
  6.     int a;
  7.     string str = "1024";
  8.     a = atoi(str.c_str());
  9.     cout << a <<endl;
  10.     return 0;
  11. }

其他相关函数还有atof,atol等。

3.使用sscanf函数(c/c++)

  1. #include <stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4.     int a;double d;
  5.     char str[] = "1024";
  6.     char strd[] = "3.1415";
  7.     sscanf(str,"%d",&a);
  8.     sscanf(strd,"%lf",&d);
  9.     printf("%d/n",a);
  10.     printf("%g/n",d);
  11.     return 0;
  12. }

4.使用c标准库中的strtol函数(c/c++)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.     int a,hex_a;double d;
  6.     char str[] = "1024";
  7.     char hex_str[] = "ff";
  8.     char strd[] = "3.1415";
  9.     a = strtol(str,NULL,10);hex_a = strtol(hex_str,NULL,16);
  10.     d =strtod(strd,NULL);
  11.     printf("%d/n",a);
  12.     printf("%d/n",hex_a);
  13.     printf("%g/n",d);
  14.     return 0;
  15. }

其他相关函数还有strtoul,将字符串转换成无符号的长整型数。

5.使用c++中的字符串流istringstream(c++)

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7.     int a;
  8.     string str = "-1024";
  9.     istringstream issInt(str);
  10.     issInt >> a;
  11.     cout << a <<endl;
  12.     return 0;
  13. }

不过,GCC(2.95.2)及以前版本并不支持sstream。

6.使用boost库中的lexical_cast函数(c++)

可以到www.boost.org下载最新的boost库,设置IDE的include路径就可以使用大部分boost功能了,具体可以参考http://www.stlchina.org/twiki/bin/view.pl/Main/BoostChina

  1. #include <boost/lexical_cast.hpp>
  2. #include <iostream>
  3. int main()
  4. {
  5.     using boost::lexical_cast;
  6.     try{
  7.     int a = lexical_cast<int>("1024");
  8.     //int a = lexical_cast<int>("xxx"); // exception
  9.     double d = lexical_cast<double>("3.14194");
  10.     std::cout<<a<<std::endl;
  11.     std::cout<<d<<std::endl;
  12.     }catch(boost::bad_lexical_cast& e){
  13.         std::cout<<e.what()<<std::endl;
  14.     }
  15.     return 0;
  16. }

    1.自己写一个函数(c/c++)

    1. #include <stdio.h>
    2. #include <assert.h>
    3. /*  my string to integer function  */
    4. int myfun(char *str){
    5.     int i = 0,n = 0,flag = 1;
    6.     if(str[0] == '-')
    7.         i = 1;flag = -1;
    8.     for(; str[i] != '/0' ; i++){
    9.         assert(str[i] >= '0' && str[i] <= '9');
    10.         n = str[i] - '0' + n*10;
    11.     }
    12.     return n*flag;
    13. }
    14. int main(int argc, char *argv[])
    15. {
    16.     int a;
    17.     char str[] = "1024";
    18.     a = myfun(str);
    19.     printf("%d/n",a);
    20.     return 0;
    21. }

    2.使用c标准库中的atoi函数(c/c++)

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. int main(int argc, char *argv[])
    4. {
    5.     int a;double d;
    6.     char str[] = "1024";
    7.     char strd[] = "3.1415";
    8.     a = atoi(str);d =atof(strd);
    9.     printf("%d/n",a);
    10.     printf("%g/n",d);
    11.     return 0;
    12. }
    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. int main(int argc, char *argv[])
    5. {
    6.     int a;
    7.     string str = "1024";
    8.     a = atoi(str.c_str());
    9.     cout << a <<endl;
    10.     return 0;
    11. }

    其他相关函数还有atof,atol等。

    3.使用sscanf函数(c/c++)

    1. #include <stdio.h>
    2. int main(int argc, char *argv[])
    3. {
    4.     int a;double d;
    5.     char str[] = "1024";
    6.     char strd[] = "3.1415";
    7.     sscanf(str,"%d",&a);
    8.     sscanf(strd,"%lf",&d);
    9.     printf("%d/n",a);
    10.     printf("%g/n",d);
    11.     return 0;
    12. }

    4.使用c标准库中的strtol函数(c/c++)

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. int main(int argc, char *argv[])
    4. {
    5.     int a,hex_a;double d;
    6.     char str[] = "1024";
    7.     char hex_str[] = "ff";
    8.     char strd[] = "3.1415";
    9.     a = strtol(str,NULL,10);hex_a = strtol(hex_str,NULL,16);
    10.     d =strtod(strd,NULL);
    11.     printf("%d/n",a);
    12.     printf("%d/n",hex_a);
    13.     printf("%g/n",d);
    14.     return 0;
    15. }

    其他相关函数还有strtoul,将字符串转换成无符号的长整型数。

    5.使用c++中的字符串流istringstream(c++)

    1. #include <iostream>
    2. #include <string>
    3. #include <sstream>
    4. using namespace std;
    5. int main(int argc, char *argv[])
    6. {
    7.     int a;
    8.     string str = "-1024";
    9.     istringstream issInt(str);
    10.     issInt >> a;
    11.     cout << a <<endl;
    12.     return 0;
    13. }

    不过,GCC(2.95.2)及以前版本并不支持sstream。

    6.使用boost库中的lexical_cast函数(c++)

    可以到www.boost.org下载最新的boost库,设置IDE的include路径就可以使用大部分boost功能了,具体可以参考http://www.stlchina.org/twiki/bin/view.pl/Main/BoostChina

    1. #include <boost/lexical_cast.hpp>
    2. #include <iostream>
    3. int main()
    4. {
    5.     using boost::lexical_cast;
    6.     try{
    7.     int a = lexical_cast<int>("1024");
    8.     //int a = lexical_cast<int>("xxx"); // exception
    9.     double d = lexical_cast<double>("3.14194");
    10.     std::cout<<a<<std::endl;
    11.     std::cout<<d<<std::endl;
    12.     }catch(boost::bad_lexical_cast& e){
    13.         std::cout<<e.what()<<std::endl;
    14.     }
    15.     return 0;
    16. }
    17. http://blog.csdn.net/alien73/article/details/3477033
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值