几种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

相关文章

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu