C字符串向数字转化
来源:互联网 发布:保卫萝卜安卓数据同步 编辑:程序博客网 时间:2024/05/18 02:16
1、
// 将科学计数法或者带小数点组成的小数字符串转化为
double atof(const char* str);
double,char* p1= "1.23";
char* p2 = "1.1E-2"; // E或e都行
atof(p1)==1.230000
atof(p2)==0.011000
2、
// 将数字组成的字符串转化为整数
int atoi(const char* str);
// 将数字组成的字符串转化为长整数
long int atol(const char* str);
// 将数字组成的字符串转化为长长整数
long long int atoll(const char* str);
char* p1 = "12a3"; atoi(p1) == 12 // 只转化字母前的数组
char* p2 = "14.56"; atoi(p2) == 14 // 非四舍五入强转
char* p3 = "12345"; atoi(p3) == 12345 // 考虑缓冲区溢出,int、long、long long数值范围越来越大
3、
// 将字符串中的小数转化为double
double strtod(const char* str, char** endptr);
// 将字符串中的小数转化为float
float strtof(const char* str, char** endptr);
// 将字符串中的小数转化为long double
long double strtold(const char* str, char** endptr);
char* str = "12.3 25.6 36.5 7.15"; // 以空格将小数隔开,不能以其它方式隔开每个小数
char* p1 = str;
char* p2 = NULL;
while(*p1 != '\0')
{
printf("%lf\n",strtod(p1, &p2));
// 第一次循环p2的内容是"25.6 36.5 7.15"
// 第二次循环p2的内容是"36.5 7.15"
// 第三次循环p2的内容是"7.15"
// 第四次循环p2的内容是'\0'
p1 = p2;
p2 = NULL;
}
4、按照2进制、8进制、10进制、16进制读取整数
long int strtol(const char* str, char** endptr, int base);
long long int strtoll(const char* str, char** endptr, int base);
unsigned long int strtoul(const char* str, char** endptr, int base);
unsigned long long int strtoull(const char* str, char** endptr, int base);
har* str = "13 0xabcd 1011 04567"; // 以空格将数隔开,不能以其它方式隔开每个数
int base[] = { 10, 16, 2, 8 }; //十进制读13,十六进制读0xabcd,二进制读1011,八进制读04567
char* p1 = str;
char* p2 = NULL;
int baseIndex = 0;
while (p1 < str+strlen(str))
{
printf("%d\n", strtol(p1, &p2, base[baseIndex++]));
p1 = p2;
p2 = NULL;
}
- C字符串向数字转化
- 字符串转化数字算法(C#)
- C语言中将数字转化为字符串
- C语言 数字转化为字符串
- C语言中将数字转化为字符串
- C语言将数字转化为字符串
- c的数字与字符串的转化
- C语言将数字转化为字符串
- C语言字符串转化为数字
- C语言数字转化为字符串
- C/C++ 数字转化为字符串函数
- C语言 字符串转化为数字
- C C++ 数字与字符串的转化
- 【C语言】数字的字符串转化为 数字
- 数字转化成字符串
- 字符串转化为数字
- 字符串转化为数字
- 字符串转化为数字
- Windows 7下通过anaconda安装tensorflow
- SurfaceFlinger简介
- Unity3d AudioSource
- oracle合并两个不一样的结果集
- Android 注解
- C字符串向数字转化
- 燕麦云何洋开讲 | 既安全又简单?我的产品设计心经分享(上)
- mapreduce的原理
- iframe jquery自适应高度(二)
- Spring与Hibernate整合的applicationContext.xml文件配置
- python 的变量和注释及简单的用户交互
- JavaScript之this学习笔记
- 判断一个链表是否有环--剑指offer
- 去除视频广告