C/C++ 中 int/long/float/double数值类型 与 字符串 互相转换
来源:互联网 发布:高斯模糊的二维算法 编辑:程序博客网 时间:2024/06/02 18:14
一、int/long/float/double 转 字符串 方法1:itoa, ltoa(a表示array数组的意思) 头文件:stdlib.h 示例: int a = 3; long b = 23; char buf1[30] = ""; 方法2:sprintf 头文件:stdio.h 示例: int a = 3; 方法3:ostringstream 头文件:#include <sstream> using namespace std; 示例: int a = 3; 二、字符串 转 int/long/float/double 方法1:atoi,atol,atof 头文件:stdlib.h 示例: int a = atoi("32"); long b = atol("333"); double c = atof("23.4"); 方法2:strtol, strtod 头文件:stdlib.h 示例: long b = strtol("333", NULL, 10);//10表示十进制 double c = strtod("32.3", NULL); 方法3:sscanf 头文件:stdio.h 示例: int a; 方法4:istringstream 头文件:#include <sstream> using namespace std; 示例: int a;
itoa(a, buf1, 10);//10表示十进制,buf1保存的内容为"3"
char buf2[30] = "";
ltoa(b, buf2, 10);//10表示十进制,buf2保存的内容为"32"
float b = 4.2f;
char buf[30] = "";
sprintf(buf, "%d,%f", a, b);//buf保存的内容为"3,4.2",可对比printf
float b = 4.2f;
ostringstream s1;
s1<<a<<","<<b;//可对比cout
string s2 = s1.str();//s2保存的内容为"3,4.2"
float b;
sscanf("23 23.4", "%d %f", &a, &b);//对比scanf
float b;
istringstream s1("23 23.4");
s1>>a>>b;//对比cin
- C/C++ 中 int/long/float/double数值类型 与 字符串 互相转换
- C/C++ 中 int/long/float/double数值类型 与 字符串 互相转换
- C/C++中int/long/float/double数值类型与字符串互相转换[总结]
- C/C++中int/long/float/double数值类型与字符串互相转换[总结]
- C/C++中int/long/float/double数值类型与字符串互相转换[总结]
- C/C++中int/long/float/double数值类型与字符串互相转换[总结]
- C/C++中int/long/float/double数值类型与字符串互相转换
- C/C++中int/long/float/double数值类型与字符串互相转换
- C/C++中int/long/float/double数值类型与字符串互相转换[总结]
- C/C++中int/long/float/double数值转换
- c语言中int long float double 等类型所占字节及输出表示
- c/c++ bytes与int,short,long,float,double相互转换
- Objective-C 字符串与数值互相转换
- Objective-C 字符串与数值互相转换
- Int,long,float,double类型
- C语言中关于float、double、long double精度及数值范围理解
- C语言中关于float、double、long double精度及数值范围理解
- C语言中关于float、double、long double精度及数值范围理解
- 有一个集合A,它又n个元素,请用回溯法输出它所有的子集。
- 二叉树创建及遍历算法(递归及非递归)
- 将16进制的字符串转换成byte数组
- 引用类型真屌
- C/C++ 中 int/long/float/double数值类型 与 字符串 互相转换
- C/C++ 中 int/long/float/double数值类型 与 字符串 互相转换
- Android TextView 支持的HTML标签
- Java Calendar Date hour
- 不同dpi下图片显示的理解的demo
- 进程按占用内存大小排序
- C#连接数据库
- C#连接数据库2
- 利用XML配置实现增删改查的.net快速开发架构 简单的构建信息管理系统架构
- 2009年全国电子设计竞赛赛题