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

2024-02-06 05:48

本文主要是介绍几种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

这篇关于几种c/c++中字符串转整形的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683314

相关文章

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方