As title states, I need a way to convert a PWCHAR to a std::string. The only solutions I can find online are for the opposite conversion, so I’d really like it if someone could shed some light on this. Thanks!
This is in c++.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
According to this MSDN page,
PWCHARis declared as follows:What you want is
std::wstring, declared instring.std::wstringis very similar tostd::string, as both are specializations ofstd::basic_string; the difference is in thatwstringuseswchar_t(WindowsWCHAR), whereasstringuseschar.If you truly want a
string(and not awstring), the advised C++ way is to useuse_facetas seen here:You may also separately convert to a multibyte C string and then use this to build your
std::string. This is not the preferred way of doing this in C++, however. The function for doing this iswcstombs, as declared below:Since you’re on Windows, you may also use
WideCharToMultiBytefor this step.LPSTRis defined as follows according to the MSDN: