Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What's the best way to convert std::wstring to numeric type, such as int, long, float or double?

like image 594
Hyden Avatar asked Feb 25 '11 14:02

Hyden


People also ask

How do I convert a string to an int in C++?

One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.

What is stoi function in C++?

What Is stoi() in C++? In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings.

How do I convert a string to a double in C++?

C++ string to float and double Conversion The easiest way to convert a string to a floating-point number is by using these C++11 functions: std::stof() - convert string to float. std::stod() - convert string to double. std::stold() - convert string to long double .


4 Answers

C++0x introduces the following functions 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 is an optionally null pointer to the end of the conversion within str (set by the conversion function).

like image 107
Howard Hinnant Avatar answered Oct 19 '22 22:10

Howard Hinnant


Either 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);

These will throw a boost::bad_lexical_cast exception if the string can't be converted.

The other option is to use Boost Qi (a sublibrary of Boost.Spirit):

#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 your compile times.

like image 30
hkaiser Avatar answered Oct 19 '22 23:10

hkaiser


Best?

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 on error handling, complex syntax, including headers by

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

It all depends what you are doing. This is the KISS way!

like image 37
ravenspoint Avatar answered Oct 19 '22 22:10

ravenspoint


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;
}
like image 3
Julio Gorgé Avatar answered Oct 19 '22 22:10

Julio Gorgé