惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
Y
Y Combinator Blog
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
S
SegmentFault 最新的问题
IT之家
IT之家
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
WordPress大学
WordPress大学
博客园 - Franky
L
LangChain Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 叶小钗
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
雷峰网
雷峰网
腾讯CDC
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Help Net Security
Help Net Security
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
博客园 - 聂微东
A
Arctic Wolf
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News

博客园 - searchDM

Elasticsearch 集成 Elasticsearch 环境 Elasticsearch 优化 Elasticsearch入门 Elasticsearch概述 吴恩达自然语言处理第一课:分类与向量空间 lucene 3.4 contrib/facet 切面搜索 在ubuntu上安装全文搜索中文分词Coreseek/sphinx及和Rails集成 solr3.4 高亮(highlight),拼写检查(spellCheck),匹配相似(moreLikeThis) 应用实践 堆与堆排序 Trie树|字典树的简介及实现 快速排序 归并排序的实现 Lucene.net搜索结果排序(单条件和多条件) 直接选择排序及交换二个数据的实现 冒泡排序 直接插入排序的三种实现 希尔排序的实现 几乎所有食物的英文翻译
Linux下C语言字符串操作之字符串转数值型
searchDM · 2013-03-30 · via 博客园 - searchDM

1,字符串转整型(一)
#include <stdlib.h>
int atoi(const char *nptr);
字符串转化为整型
long atol(const char *nptr);
字符串转化为长整型
long long atoll(const char *nptr);
long long atoq(const char *nptr);
字符串转化为long long 类型
英文手册很简单,直接上
说明:The atoi() function converts the initial portion of the string pointed to by nptr to int.  The behavior is the same as strtol(nptr, (char **) NULL, 10); except that atoi() does not detect errors. The  atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long.  atoq() is an obsolete name for atoll().

2,字符串转整型(二)
#include <stdlib.h>
long int strtol(const char *nptr, char **endptr, int base);
字符串转长整型,base参数为进制,如转化为10进制,则base应该为10
long long int strtoll(const char *nptr, char **endptr, int base);
字符串转化为long long int
说明:详细说明请参考man手册。

3,字符串转浮点数
#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
字符串转 双精度浮点数 double 类型
float strtof(const char *nptr, char **endptr);
字符串转 单精度浮点数 float 类型
long double strtold(const char *nptr, char **endptr);
字符串转 long double 类型

有了以上库函数,可以很方便的把字符串转化为数值型,真系灰常的方便啊,有木有?

示例代码:

#include <stdio.h>
#include <stdlib.h>
int main()

{

  char *str_int="892";
  int int_val=atoi(str_int);
  printf("字符串转整型:%d\n",int_val);
  long long_val=atol(str_int);
  printf("字符串转长整型:%ld\n",long_val);
  char *str_float="238.23";
  char *endptr;
  float float_val=strtof(str_float,&endptr);
  printf("字符串转单精度浮点型:%f\n",float_val);
  double double_val=strtod(str_float,&endptr);
  printf("字符串转双精度浮点型:%f\n",double_val);
  char *str_long="9839282";
  int base=10;
  long long_v=strtol(str_long,&endptr,base);
  printf("strtol : %ld\n",long_v);
  return 0;
}

代码输出:

  1. 字符串转整型:892
  2. 字符串转长整型:892
  3. 字符串转单精度浮点型:238.229996
  4. 字符串转双精度浮点型:238.230000
  5. strtol : 9839282


说明:以上各函数的man手册都有详尽的解释,详情请查阅man手册。