c++ – Convert std::string to std::wstring


How to convert std::string to std::wstring ?

Let's say:

std::string s = "some string";
std::wstring ws = f(s);

How can f() be implemented?


There are many ways… here are a couple…

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string str = converter.to_bytes(L"Hello world");
std::wstring wstr = converter.from_bytes("Hello world");

std::string str("Hello world!!!");
std::wstring wstr(str.begin(), str.end());
Scroll to Top