Skip to content

Instantly share code, notes, and snippets.

@oorlykk
Forked from gchudnov/cpp_utf8_utf16.cpp
Created December 30, 2019 08:51
Show Gist options
  • Select an option

  • Save oorlykk/f236b69d48b28b3cc5f7846fb7fd76ed to your computer and use it in GitHub Desktop.

Select an option

Save oorlykk/f236b69d48b28b3cc5f7846fb7fd76ed to your computer and use it in GitHub Desktop.
C++ string conversion UTF8 <-> UTF16
#include <string>
#include <locale>
#include <codecvt>
//UTF-8 to UTF-16
std::string source;
//...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string dest = convert.from_bytes(source);
//UTF-16 to UTF-8
std::u16string source;
//...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::string dest = convert.to_bytes(source);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment