std::string
to std::wstring
and back again
I was using this code for Windows development but found that
winrt::to_hstring
handled the cases I needed. This code may still be useful
for Win32API development using functions like CreateFileW
or SendMessageW
.
#include <string>
#include <locale>
#include <codecvt>
std::string strutils_wstring_to_string(const std::wstring & input) {
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(input);
}
std::wstring strutils_string_to_wstring(const std::string & input) {
using convert_typeX = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(input);
}