几种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++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键