After reading Is <codecvt>
not a standard header? I am not sure what to do as my Windows version of the codebase uses <codecvt>
to convert between wide strings and strings. I currently use GCC 4.7 for Linux version of my code. Is <codecvt>
also missing in the latest GCC? What would be a workaround?
BTW, as it's stated here the following code wouldn't work with GCC:
wstring ws = L"hello";
string ns(ws.begin(), ws.end());
How about using mbsrtowcs
and wcsrtombs
? Though they come from C are not very convenient to use with std::string
and std::wstring
(but you can always make your own convenient C++ functions based on them). Besides, the conversion is performed based on the currently installed C locale. So, your example code snippet can be changed to
something like:
std::mbstate_t state = {};
const wchar_t* p = L"hello";
std::string ns(std::wcsrtombs(NULL, &p, 0, &state) + 1);
std::wcsrtombs(&ns[0], &p, ns.size(), &state);
ns.pop_back();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With