背景小知识:
例如 int、long 和 double 等,有时也需要转换为字符串形式,这样结果字符串才能立即输出到文件或其他输入输出设备,或者存入内存中的某个字符串对象,供以后使用。
当用户在键盘上输入一个数字时,该数字以字符串的形式输入,就好像用户输入的一系列字符(数字)。在 C++ 中,这样的数字通常通过流提取运算符 >> 来读取。在存储到数字类型的变量之前,该运算符会根据需要自动执行转换。在输出期间,数字到字符串的反向转换由流输出运算符 << 执行。
简单的可以参考我另一篇博客C++实现数据反式转换为字符串格式
与本篇博客相对应的是下一篇字符串与数字格式转换__数字格式转换成字符串转换的函数(2)
介绍4个方法:
1 利用sscanf() 将字符串转化为数字
#include <iostream>
#include <string>
using namespace std;
// sscanf() 用于将字符串转化为数字
int main()
{
char str[]="1234321";
int a;
sscanf(str,"%d",&a);
cout<<a<<endl;
char str1[]="908.456";
double b;
sscanf(str1,"%lf",&b);
cout<<b<<endl;
}
详细请看百度百科
2. 利用stringstream
字符串格式转换 ss >>
过程:string -> stringstream对象 -> 数字
#include <string>
#include <sstream>
#include<iostream>
using namespace std;
int main(){
double d ;
string res= "-1";
stringstream ss;
//string->double
ss << res;
ss >> d;
cout<<"d="<<d<<endl;
//string->int
ss.clear();//stringstream对象清零
int i;
ss<<res;
ss>>i;
cout<<"i="<<i<<endl;
//string->unsigned int
ss.clear();//stringstream对象清零
unsigned int ui;
ss<<res;
ss>>ui;
cout<<"ui="<<ui<<endl;
return 0;
}
3、 atof()
是C 语言标准库中的一个字符串处理函数,功能是把字符串转换成浮点数,所使用的头文件为<stdlib.h>。该函数名是 “ascii to floating point numbers” 的缩写。语法格式为:double atof(const char *nptr)。
功能: 把字符串转换成浮点数
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
double d;
char str[] = "123.456";
d=atof(str);
printf("string=%sdouble=%lf\n",str,d);
cout<<str<<endl;
cout<<d<<endl;
return 0;
}
4.stoi(str, &pos); 转换函数
/数字转换函数1.// This program demonstrates the use of the stoXXX()
// numeric conversion functions.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str; // string to convert
size_t pos; // Hold position of stopping character
//Convert string to double
str = "-342.57is a number";
cout << "The string is " << str << endl;
double d = stod(str, &pos);
cout << "The converted double is " << d << endl;
cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
// Convert string to int (default to decimal)
str = "-342.57is a number";
cout << "\nThe string is " << str << endl;
int i = stoi(str, &pos);
cout << "The converted integer is " << i << endl;
cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
// Convert string to int (base is binary)
str = "01110binary number";
cout << "\nThe string is " << str << endl;
i = stoi(str, &pos, 2);
cout << "The converted binary integer is " << i << endl;
cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
return 0;
}
string str; // string to convert
size_t pos; // Hold position of stopping character
stoi(str, &pos); 转换函数
5.istringstream和ostringstream
//字符串向数字转化
//This program illustrates the use of sstream objects
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "John 20 50"; // string to read from
const char *cstr = "Amy 30 42"; // Cstring to read from
istringstream istr1 (str); // istr1 will read from str
istringstream istr2; // istr2 will read from cstr
ostringstream ostr; // The ostringstream object to write to
string name;
int score1, score2, average_score;
// Read name and scores and compute average then write to ostr
istr1 >> name >> score1 >> score2;
average_score = (score1 + score2)/2;
ostr << name << " has average score" << average_score << "\n";
// Set istr2 to read from the C string and repeat the above
istr2.str(cstr);
istr2 >> name >> score1 >> score2;
average_score = (score1 + score2)/2;
ostr << name << " has average score" << average_score << "\n";
// Switch to hexadeximal output on ostr
ostr << hex;
// Write Amy's scores in hexadecimal
ostr << name << "'s scores in hexadecimal are: " << score1 << " and " << score2 << "\n";
// Extract the string from ostr and print it to the screen
cout << ostr.str();
return 0;
}