How to do it...

Use the following standard conversion functions when you need to convert between numbers and strings:

  • To convert from an integer or floating point type to a string type, use std::to_string() or std::to_wstring() as shown in the following code snippet:
        auto si = std::to_string(42);      // si="42" 
auto sl = std::to_string(42l); // sl="42"
auto su = std::to_string(42u); // su="42"
auto sd = std::to_wstring(42.0); // sd=L"42.000000"
auto sld = std::to_wstring(42.0l); // sld=L"42.000000"
  • To convert from a string type to an integer type, use std::stoi(), std::stol(), std::stoll(), std::stoul(), or std::stoull(); refer to the following code snippet:
        auto i1 = std::stoi("42");                 // i1 = 42 
auto i2 = std::stoi("101010", nullptr, 2); // i2 = 42
auto i3 = std::stoi("052", nullptr, 8); // i3 = 42
auto i4 = std::stoi("0x2A", nullptr, 16); // i4 = 42
  • To convert from a string type to a floating point type, use std::stof(), std::stod(), or std::stold(), as shown in the following code snippet:
        // d1 = 123.45000000000000 
auto d1 = std::stod("123.45");
// d2 = 123.45000000000000
auto d2 = std::stod("1.2345e+2");
// d3 = 123.44999980926514
auto d3 = std::stod("0xF.6E6666p3");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset