How to convert std :: wstring to a numeric type (int, long, float)?

What is the best way to convert std :: wstring to a numeric type like int, long, float or double?

+6
c ++ types stl wstring
source share
6 answers

Use boost::lexical_cast<> :

 #include <boost/lexical_cast.hpp> std::wstring s1(L"123"); int num = boost::lexical_cast<int>(s1); std::wstring s2(L"123.5"); double d = boost::lexical_cast<double>(s2); 

They will throw a boost::bad_lexical_cast exception if the string cannot be converted.

Another option is to use Boost Qi (Boost.Spirit sub-library):

 #include <boost/spirit/include/qi.hpp> std::wstring s1(L"123"); int num = 0; if (boost::spirit::qi::parse(s1.begin(), s1.end(), num)) ; // conversion successful std::wstring s2(L"123.5"); double d = 0; if (boost::spirit::qi::parse(s1.begin(), s1.end(), d)) ; // conversion successful 

Using Qi is much faster than lexical_cast, but will increase compilation time.

+17
source share

C ++ 0x represents after function in <string> :

 int stoi (const wstring& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10); unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10); long long stoll (const wstring& str, size_t* idx = 0, int base = 10); unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10); float stof (const wstring& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); long double stold(const wstring& str, size_t* idx = 0); 

idx - an optional null pointer to the end of the conversion inside str (specified by the conversion function).

+30
source share

it's better?

If you don’t want to use anything more than the CRT library and are happy with getting 0, if the string cannot be converted, then you can save complex syntax for error handling, including headers

 std::wstring s(L"123.5"); float value = (float) _wtof( s.c_str() ); 

It all depends on what you do. This is the KISS way!

+11
source share

Use wstringstream / stringstream :

 #include <sstream> float toFloat(const std::wstring& strbuf) { std::wstringstream converter; float value = 0; converter.precision(4); converter.fill('0'); converter.setf( std::ios::fixed, std::ios::floatfield ); converter << strbuf; converter >> value; return value; } 
+3
source share

just use stringstream: don't forget #include <sstream>

 wchar_t blank; wstring sInt(L"123"); wstring sFloat(L"123.456"); wstring sLong(L"1234567890"); int rInt; float rFloat; long rLong; wstringstream convStream; convStream << sInt<<' '<< sFloat<<' '<<sLong; convStream >> rInt; convStream >> rFloat; convStream >> rLong; cout << rInt << endl << rFloat << endl << rLong << endl; 
0
source share

So, I used Embarcadero, and this piece ..... did not allow me to use stoi, so I need to create my own function.

 int string_int(wstring lala){ int numero; int completo = 0; int exponente = 1; int l = 1; for (int i = 0; i < lala.size(); i++){ numero = 0; for (int j = 48; j < 57; j++){ if (lala[i] == j){ break; } numero++; } for (int k = 0; k < lala.size() - l; k++){ exponente *= 10; } numero *= exponente; completo += numero; exponente = 1; l++; } return completo; } 
-one
source share

All Articles