C/C++中int/long/float/double数值类型与字符串互相转换
来源:互联网 发布:一致性哈希算法的用途 编辑:程序博客网 时间:2024/05/22 03:02
memcpy方法可以实现将int等保存到字符类型的数组中。
示例:
long long_data=-9828;
unsigned char data[4];
memcpy(data,&t,4); //将long类型的数据用4个char保存。
long my_long_data=0;
memcpy(&tt,data,4);//从4个char中还原出long类型数据。
一、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;
- 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精度及数值范围理解
- MySQL 5.6 复制介绍
- 冠群总裁刘广东出席互联网金融高端研讨会
- Linux 3.x 内核学习笔记——页面的周转
- 如何搭建安全的WEB服务器
- 卷积
- C/C++中int/long/float/double数值类型与字符串互相转换
- spring技术详解
- 宏的一些小技巧用法,控制打印输出,随机色
- TFS编译
- 【EasyUI】combotree和combobox模糊查询
- SDWebImage内部实现过程
- ios开发-popToViewController视图操作
- 机器学习和计算机视觉相关的数学
- 配置ssh以及ssh的安全管理