converting string to a double in visual c++ by parsing

682 views Asked by At

In c# it is easy to use try parse method,but how to use this in visual c++. I tried to use ToDouble, Parse...,but they are not recognized by visual studio by default. if there is some namespace i should add to apply this please tell..

1

There are 1 answers

0
Ben Voigt On BEST ANSWER

Your Windows phone project is being developed using C++/CX which is the C++ interface to WinRT.

C++/CX represents string data using Platform::String^, a handle to a WinRT HSTRING. These strings store arrays of Unicode characters, and you can get a wchar_t* via s->Data().

Then you just need a C++ function that parses numeric data from a wchar_t*. wcstod, which is the wide character version of strtod, is a great choice.

So your final code will end up looking something like:

wchar_t* end_parse;
double value = wcstod(s->Data(), &end_parse);

You can check whether the parse ended without processing the whole string, by looking at *end_parse. Typically a successful parse will result in *end_parse == L'\0', but if you are processing comma-separated values you might require that *end_parse == L',', and so on.