【C++】字符串和数字互相转换大全

一、使用字符串流对象进行数字转换

C++ 有两个类,ostringstream 和 istringstream,可以用来对内存中的值执行字符串/数字转换。必须在程序中包含 sstream 头文件才能使用这些类。

以下是这些类的用法:

//This program illustrates the use of sstream objects
#include 
#include 
#include 
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;
    /* 结果如下:
	John has average score35
	Amy has average score36
	Amy's scores in hexadecimal are: 1e and 2a
	*/
}
二、数字转换函数:

1. 数字转换为字符串:
C++ 11 提供了若干 to_string(T value) 函数来将 T 类型的数字值转换为字符串形式。以下是几个 to_string() 函数的列表:

  1. string to_string(int value)
  2. string to_string(long value)
  3. string to_string(double value)

实例为:

int a = 5;
string str1 = to_string(a*a);
string str2 = to_string(ob1001);
cout << " The square of 5 is " << str1 << endl;
cout<< str2 <<endl;
/* 结果为:
	The square of 5 is 25
   	9
*/

注意⚠️:to_string() 函数无法处理非十进制整数的转换。如果需要该功能,则应该使用 ostringsteam 对象来完成该转换。

c++中的二进制、八进制和十六进制的表达方法是:
1. 二进制
例:intx=0b1001; //x=9
2. 八进制
例:inty=074; //x=60
3. 十六进制
例:intz=0xa3; //x=163;

2. 字符串转换为数字:
字符串到数字的转换可以通过 stoX() 系列函数来执行。该系列函数的成员可以将字符串转换为 int、long、float 和 double 类型的数字。具体语法如下所示:

  1. int stoi(const strings str, size_t* pos = 0, int base = 10)
  2. long stol(const strings str, size_t* pos = 0, int base = 10)
  3. float stof(const strings str, size_t* pos = 0)
  4. double stod(const strings str, size_t* pos = 0)

注⚠️:pos 中保存了 str 无法被转换的第一个字符的索引,base 形参仅适用于整数转换,指示用于转换的进制。

// This program demonstrates the use of the stoXXX()
// numeric conversion functions.
#include 
#include 
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;
}

/*
结果是:
The string is -342.57is a number
The converted double is -342.57
The stopping character is i at position 7

The string is -342.57is a number
The converted integer is -342
The stopping character is . at position 4

The string is 01110binary number
The converted binary integer is 14
The stopping character is b at position 5
*/

你可能感兴趣的:(C++知识圈,c++,c语言,字符串)