fork(14) download
  1. #include <iostream>
  2. #include <string>
  3. #include <locale>
  4. #include <codecvt>
  5. int main()
  6. {
  7. // u8"z水�" aka u8"z\u6c34\U0001d10b"
  8. std::string utf8 = "\x7a\xe6\xb0\xb4\xf0\x9d\x84\x8b";
  9.  
  10. // the UTF-8 / UTF-16 standard conversion facet
  11. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv;
  12. std::u16string utf16 = utf16conv.from_bytes(utf8);
  13. std::cout << "UTF16 conversion produced " << utf16.size() << " code points:\n";
  14. for(auto p = utf16.begin(); p!=utf16.end(); ++p)
  15. std::cout << std::hex << std::showbase << *p << '\n';
  16.  
  17. // the UTF-8 / UCS2 standard conversion facet
  18. std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> ucs2conv;
  19. try {
  20. std::u16string ucs2 = ucs2conv.from_bytes(utf8);
  21. // Visual Studio 2010 does this
  22. std::cout << "UCS2 completed after producing " << std::dec << ucs2.size()<<" characters:\n";
  23. for(auto p = ucs2.begin(); p!=ucs2.end(); ++p)
  24. std::cout << std::hex << std::showbase << *p << '\n';
  25. } catch(const std::range_error& e) {
  26. // clang++/libc++ does this
  27. std::u16string ucs2 = ucs2conv.from_bytes(utf8.substr(0, ucs2conv.converted()));
  28. std::cout << "UCS2 failed after producing " << std::dec << ucs2.size()<<" characters:\n";
  29. for(auto p = ucs2.begin(); p!=ucs2.end(); ++p)
  30. std::cout << std::hex << std::showbase << *p << '\n';
  31. }
  32.  
  33. // the UTF-8 / UTF-32 standard conversion facet
  34. std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
  35. std::u32string utf32 = utf32conv.from_bytes(utf8);
  36. std::cout << "UTF32 conversion produced " << std::dec << utf32.size() << " characters:\n";
  37. for(auto p = utf32.begin(); p!=utf32.end(); ++p)
  38. std::cout << std::hex << std::showbase << *p << '\n';
  39. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:19: fatal error: codecvt: No such file or directory
compilation terminated.
stdout
Standard output is empty