#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
int main()
{
// u8"z水�" aka u8"z\u6c34\U0001d10b"
std::string utf8 = "\x7a\xe6\xb0\xb4\xf0\x9d\x84\x8b";
// the UTF-8 / UTF-16 standard conversion facet
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv;
std::u16string utf16 = utf16conv.from_bytes(utf8);
std::cout << "UTF16 conversion produced " << utf16.size() << " code points:\n";
for(auto p = utf16.begin(); p!=utf16.end(); ++p)
std::cout << std::hex << std::showbase << *p << '\n';
// the UTF-8 / UCS2 standard conversion facet
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
try {
std::u16string ucs2 = ucs2conv.from_bytes(utf8);
// Visual Studio 2010 does this
std::cout << "UCS2 completed after producing " << std::dec << ucs2.size()<<" characters:\n";
for(auto p = ucs2.begin(); p!=ucs2.end(); ++p)
std::cout << std::hex << std::showbase << *p << '\n';
} catch(const std::range_error& e) {
// clang++/libc++ does this
std::u16string ucs2 = ucs2conv.from_bytes(utf8.substr(0, ucs2conv.converted()));
std::cout << "UCS2 failed after producing " << std::dec << ucs2.size()<<" characters:\n";
for(auto p = ucs2.begin(); p!=ucs2.end(); ++p)
std::cout << std::hex << std::showbase << *p << '\n';
}
// the UTF-8 / UTF-32 standard conversion facet
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
std::u32string utf32 = utf32conv.from_bytes(utf8);
std::cout << "UTF32 conversion produced " << std::dec << utf32.size() << " characters:\n";
for(auto p = utf32.begin(); p!=utf32.end(); ++p)
std::cout << std::hex << std::showbase << *p << '\n';
}