#include <codecvt>
#include <iostream>
#include <locale>
int main()
{
char utf8[4] = {'\xf0','\x9d','\x84','\x8b'}; // Segno, U+1d10b
const char* utf8_next;
// the UTF-8 to UTF-16
std::mbstate_t state16;
char16_t utf16[2];
char16_t* utf16_next;
auto& f1 = std::use_facet<std::codecvt<char16_t, char, std::mbstate_t>>(std::locale::classic());
// <codecvt> alternative: std::codecvt_utf8_utf16<char16_t> f1;
f1.in(state16, utf8, utf8+4, utf8_next, utf16, utf16+2, utf16_next);
std::cout << "UTF16 conversion: processed " << (utf8_next - utf8) << " UTF-8 bytes\n";
for(char16_t* p = utf16; p != utf16_next; ++p)
std::cout << std::hex << std::showbase << *p << '\n';
// UTF-8 to UTF-32
std::mbstate_t state32;
char32_t utf32[1];
char32_t* utf32_next;
auto& f2 = std::use_facet<std::codecvt<char32_t, char, std::mbstate_t>>(std::locale::classic());
// <codecvt> alternative: std::codecvt_utf8<char32_t> f2;
f2.in(state32, utf8, utf8+4, utf8_next, utf32, utf32+1, utf32_next);
std::cout << "UTF32 conversion: processed " << std::dec << (utf8_next - utf8) << " UTF-8 bytes\n";
for(char32_t* p = utf32; p != utf32_next; ++p)
std::cout << std::hex << std::showbase << *p << '\n';
}
I2luY2x1ZGUgPGNvZGVjdnQ+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGxvY2FsZT4KaW50IG1haW4oKQp7CiAgICBjaGFyIHV0ZjhbNF0gPSB7J1x4ZjAnLCdceDlkJywnXHg4NCcsJ1x4OGInfTsgLy8gU2Vnbm8sIFUrMWQxMGIKICAgIGNvbnN0IGNoYXIqIHV0ZjhfbmV4dDsKCiAgICAvLyB0aGUgVVRGLTggdG8gVVRGLTE2CiAgICBzdGQ6Om1ic3RhdGVfdCBzdGF0ZTE2OwogICAgY2hhcjE2X3QgdXRmMTZbMl07CiAgICBjaGFyMTZfdCogdXRmMTZfbmV4dDsKICAgIGF1dG8mIGYxID0gc3RkOjp1c2VfZmFjZXQ8c3RkOjpjb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