fork download
  1. #include <codecvt>
  2. #include <iostream>
  3. #include <locale>
  4. int main()
  5. {
  6. char utf8[4] = {'\xf0','\x9d','\x84','\x8b'}; // Segno, U+1d10b
  7. const char* utf8_next;
  8.  
  9. // the UTF-8 to UTF-16
  10. std::mbstate_t state16;
  11. char16_t utf16[2];
  12. char16_t* utf16_next;
  13. auto& f1 = std::use_facet<std::codecvt<char16_t, char, std::mbstate_t>>(std::locale::classic());
  14. // <codecvt> alternative: std::codecvt_utf8_utf16<char16_t> f1;
  15. f1.in(state16, utf8, utf8+4, utf8_next, utf16, utf16+2, utf16_next);
  16. std::cout << "UTF16 conversion: processed " << (utf8_next - utf8) << " UTF-8 bytes\n";
  17. for(char16_t* p = utf16; p != utf16_next; ++p)
  18. std::cout << std::hex << std::showbase << *p << '\n';
  19.  
  20. // UTF-8 to UTF-32
  21. std::mbstate_t state32;
  22. char32_t utf32[1];
  23. char32_t* utf32_next;
  24. auto& f2 = std::use_facet<std::codecvt<char32_t, char, std::mbstate_t>>(std::locale::classic());
  25. // <codecvt> alternative: std::codecvt_utf8<char32_t> f2;
  26. f2.in(state32, utf8, utf8+4, utf8_next, utf32, utf32+1, utf32_next);
  27. std::cout << "UTF32 conversion: processed " << std::dec << (utf8_next - utf8) << " UTF-8 bytes\n";
  28. for(char32_t* p = utf32; p != utf32_next; ++p)
  29. std::cout << std::hex << std::showbase << *p << '\n';
  30. }
  31.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
UTF16 conversion: processed 4 UTF-8 bytes
0xd834
0xdd0b
UTF32 conversion: processed 4 UTF-8 bytes
0x1d10b