fork download
  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void print_as16(unsigned utf32) {
  6. if ( utf32 < 0x10000 )
  7. printf("\\u%04X", utf32);
  8. else {
  9. unsigned wwwww = (utf32 >> 16) - 1;
  10. unsigned x1 = (utf32 >> 10) & 0x3F;
  11. unsigned x2 = (utf32 >> 0) & 0x03FF;
  12. unsigned utf16_1 = 0xD800 | (wwwww << 6) | x1;
  13. unsigned utf16_2 = 0xDC00 | x2;
  14. printf("\\u%02X\\u", utf16_1, utf16_1);
  15. }
  16. }
  17.  
  18. int main() {
  19. char const *p_org="\xE3\x81\xBE\xE3\x82\x93\xE3\x81\x93z"; // UTF-8文字列。
  20.  
  21. for ( auto i = 0; i < strlen(p_org); i++ ) {
  22. if ( (p_org[i] & 0x80) == 0 )
  23. printf("%c", p_org[i]);
  24. else if ( (p_org[i] & 0xE0) == 0xC0 ) {
  25. unsigned yyyyy = p_org[i] & 0x1F;
  26. unsigned xxxxxx = p_org[++i] & 0x3F;
  27. unsigned utf32 = (yyyyy<<6) | xxxxxx;
  28. print_as16(utf32);
  29. }
  30. else if ( (p_org[i] & 0xF0) == 0xE0 ) {
  31. unsigned zzzz = p_org[i] & 0x0F;
  32. unsigned yyyyyy = p_org[++i] & 0x3F;
  33. unsigned xxxxxx = p_org[++i] & 0x3F;
  34. unsigned utf32 = (zzzz<<12) | (yyyyyy<<6) | xxxxxx;
  35. print_as16(utf32);
  36. }
  37. else if ( (p_org[i] & 0xF0) == 0xF0 ) ; // 4バイトは省略
  38. }
  39.  
  40. getchar();
  41. }
  42.  
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
\u307E\u3093\u3053z