参考 C++ 数字与字符串之间相互转换(多种方法)
参考 c++ 字符串与数字相互转换
1. 字符串转数字 C函数 atoi()、atof()
atoi() (表示 ascii to integer)把字符串转换成整型数的一个函数,,头文件为<stdlib.h>。
int atoi(const char *nptr) 函数会扫描参数 nptr字符串,会跳过前面的空白字符(例如空格,tab缩进)等。
如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回 0 。
atof() 把字符串转换成浮点数,头文件为<stdlib.h>。
该函数名是 “ascii to floating point numbers” 的缩写。
语法格式为:double atof(const char *nptr)。
atoi, atof,atol函数,均在stdlib.h中进行了声明,在头文件中,其声明的函数原型为
double atof(const char *);
int atoi(const char *);
long atol(const char *);
不难看出,上面这些方法的输入均为const char *,即字符串,得到的输出为转化以后的各种数值类型。
实例
#include<stdio.h>
#include<stdlib.h>
void main() {
char s1[] = "123";
char s2[] = "123.1";
int i = atoi(s1);
double d = atof(s2);
printf("%d %5.1f\n",i,d);
}
2. 字符串转数字 C++
(1)stoi()、stol()、stof()
这些函数均位于string类中,函数签名原型为
_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
实例
#include<iostream>
#include<string>
using namespace std;
void main() {
string s1 = "456";
string s2 = "456.2";
int i = stoi(s1);
long l = stol(s1);
float f = stof(s2);
double d = stod(s2);
cout << i << endl;
cout << l << endl;
cout << f << endl;
cout << d << endl;
}
输出
456
456
456.2
456.2
##(2)、使用atoi()
char str3[10] = "3245345";
//数字简单,所以转数字一个参数
long long a = atoi(str3);
cout << a << endl;
(3)、使用 sscanf() 映射
long long c = 0;
char str5[10] = "661234544";
sscanf(str5, "%d", &c); //从左至右,字符串转数字
cout << c << endl;
(4)、自己写一个简单的
//字符串转为整数,通过减'0'字符,底层用ASCII码相减
void myAtoi(char str[],long long& m){
int i(0);
int temp = 0;
while(str[i] != '\0'){
temp = temp*10 + (str[i] -'0');
++i;
}
m = temp; //转换后赋值给m
}
3.数字转字符串
c++11以后,string类中提供了to_string方法,可以将各种类型数字转成字符串。
(1)、使用c++里的to_string()
long long m = 1234566700;
string str = to_string(m); //系统提供数字转字符
cout << str << endl;
实例
void main() {
int i = 123;
float f = 1.234;
double d = 2.012;
cout << to_string(i) << endl;
cout << to_string(f) << endl;
cout << to_string(d) << endl;
}
可以看到输入长度,不可控制;
为了控制浮点数的精度,可以使用ostringstream来控制精度。
头文件
#include<sstream>
#include<iostream>
#include<string>
using namespace std;
void main() {
float f = 1.234;
double d = 2.01;
stringstream sf,sd;
sf.precision(5);
sf << f;
cout << sf.str() << endl;
sd.precision(5);
sd << d;
cout << sd.str() << endl;
}
(2)、使用itoa()
int n = 100;
char str2[10];
//字符串比较麻烦,所以转字符串三个参数,我是这么记得(手动滑稽)
itoa(n,str2,10); //第一个参数为整数,第二个为字符串(char*),第三个为进制
cout << str2 << endl;
(3)、使用sprintf() 映射
long long b = 1234560;
char str4[10] = {0};
sprintf(str4, "%d", b); //从右至左,把数转换为字符串
cout << str4 << endl;
(4)、自己写一个简单的
//整数转为字符串:通过加 '0'字符
void myItoa(long long n, char str[]){
char temp[MAX]{0};
int i(0);
int j = 0;
while(n){
temp[i++] = n%10 + '0';
n /= 10;
}
//此时为逆序,需要调整为正序
//cout << temp << endl;
while(i>0)
str[j++] = temp[--i];
//cout << str << endl;
}