Convert std:: string to std:: wstring
How do I translate std::string
to std::wstring
?
Admit:
std::string s = "some string";
std::wstring ws = f(s);
How can f()
be implemented?
10
Author: Arhadthedev, 2014-06-26
2 answers
Many ways..., here are a couple...
//1
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");
//2
std::string str("Hello world!!!");
std::wstring wstr(str.begin(), str.end());
18
Author: devEugene, 2014-06-26 16:02:09
In C++17, the header <codecvt>
became deprecated, as well as the template classes std::wstring_convert
and std::wbuffer_convert
, so at the moment there is no way to do this in the general case without using third-party libraries (or outdated functionality).
1
Author: Tocic, 2018-11-24 14:18:08