Convert std::string to WCHAR ?

Started by
20 comments, last by iMalc 10 years, 10 months ago

If the input string is ASCII, you can easily expand the string using the methods given above. However if the input string is unicode, that won't work.

Principally, it does:


std::wstring wide(L"does not contain special UTF letters");
std::string multi(wide.begin(), wide.end();
std::cout << multi.c_str();

I used this to convert a wstring for output with a filestream. Though I didn't try it out with "special" UTF-chars, it surely won't work with them, would be interesting whats the result though...

Advertisement

To be honest, with the codebase I work on in MSVS, I just use _bstr_t to convert, which has constructors taking either type, and operators returning either type.

It turns this into a trivial one-liner. E.g.


std::wstring toWString(const std::string &as) { return _bstr_t(as); }
std::string toAString(const std::wstring &ws) { return _bstr_t(ws); }

http://msdn.microsoft.com/en-us/library/btdzb8eb(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/9k3ebasf(v=vs.71).aspx

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement