Can I get a wchar_t from a std::string?

U

Uday Bidkar

I can see how to get a char* but is it possible to get a wide char -
eg
wchar_t?

std::string is a specialization of class basic_string with elements of
type char as a string.Hence std::string::c_str() would return you
char*. You can use MultiByteToWideChar function to convert this char
string into wchar_t string.

Instead use std::wstring which is a specialization of class
basic_string with elements of type wchar_t as a string and
std::wstring::c_str() would return you wchar_t*.You can use
WideCharToMultiByte function to convert this wide char string into
char string.
 
G

Gavin Deane

std::string is a specialization of class basic_string with elements of
type char as a string.Hence std::string::c_str() would return you
char*. You can use MultiByteToWideChar function to convert this char
string into wchar_t string.

Instead use std::wstring which is a specialization of class
basic_string with elements of type wchar_t as a string and
std::wstring::c_str() would return you wchar_t*.You can use
WideCharToMultiByte function to convert this wide char string into
char string.

MultiByteToWideChar and WideCharToMultiByte are not standard C++
functions. If they are provided by your implementation then they are
an extension. If they do what you need then use them, but understand
that the next compiler you use may not provide them.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top