jeudi 4 juin 2015

Convert UTF8 string to wchar_t/std::wstring using codecvt_utf8 in C++11

As I understand it, in C++11 there are now built-in facilities to convert between UTF8 and wchar_t.

I found codecvt_utf8, which appears to do that, but the syntax is not very obvious - it's a template function. After reading their example code, I'm still not sure what the correct syntax is.

If I want to have those 2 functions, how would I fill them, using codecvt_utf8?

std::wstring fromUtf8(const char *utf8) {
    ...
}

std::string toUtf8(const wchar_t *wch) {
    ...
}

I found UTF8 to/from wide char conversion in STL, but it's very general, and it doesn't assume C++11 and codecvt_utf8. As I understand it, before C++11 there was no real portable way to do such conversions.

Aucun commentaire:

Enregistrer un commentaire