fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void output(const u16string &s)
  7. {
  8. cout << "length: " << s.length() << endl;
  9.  
  10. cout << "code units: ";
  11. for (auto ch : s)
  12. cout << hex << setw(4) << setfill('0') << (int)ch << ' ';
  13. cout << endl;
  14.  
  15. cout << "bytes: ";
  16. const unsigned char *ptr = reinterpret_cast<const unsigned char*>(s.data());
  17. for (size_t i = 0; i < s.length() * sizeof(char16_t); ++i)
  18. cout << hex << setw(2) << setfill('0') << (int)ptr[i] << ' ';
  19. cout << endl;
  20. }
  21.  
  22. int main() {
  23. output(u16string{u"万不得已"});
  24. cout << endl;
  25. output(u16string{u"\007\116\015\116\227\137\362\135"});
  26. return 0;
  27. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
length: 4
code units: 4e07 4e0d 5f97 5df2 
bytes: 07 4e 0d 4e 97 5f f2 5d 

length: 8
code units: 0007 004e 000d 004e 0097 005f 00f2 005d 
bytes: 07 00 4e 00 0d 00 4e 00 97 00 5f 00 f2 00 5d 00