fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <locale>
  4. #include <codecvt>
  5. using namespace std;
  6.  
  7. template <typename O, typename S>
  8. void write_hex(O &out, const S &s) {
  9. for (auto x : s) {
  10. out << std::hex << static_cast<uint16_t>(x) << " ";
  11. }
  12. out << endl;
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18. wstring_convert<codecvt_utf8<char16_t>, char16_t> cvu16;
  19. wstring_convert<codecvt_utf8<wchar_t>> cvu32;
  20.  
  21. cout << "string:" << endl;
  22. string str = u8"abecą";
  23. cout << str << endl;
  24. write_hex(cout, str);
  25. cout << endl;
  26.  
  27. cout << "u16string:" << endl;
  28. u16string str_u16 = u"abecą";
  29. cout << cvu16.to_bytes(str_u16) << endl;
  30. write_hex(cout, str_u16);
  31. cout << endl;
  32.  
  33. cout << "wstring:" << endl;
  34. wstring str2 = L"abecą";
  35. wcout << str2 << endl;
  36. cout << cvu32.to_bytes(str2) << endl;
  37. write_hex(cout, str2);
  38. cout << endl;
  39. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
string:
abecą
61 62 65 63 ffc4 ff85 

u16string:
abecą
61 62 65 63 105 

wstring:
abec
abecą
61 62 65 63 105