简体   繁体   English

我如何打印UTF-8字符C ++?

[英]How I print UTF-8 characters C++?

我如何在C ++中打印这些UTF-8字符

Just output the appropriate bytes to your terminal, and make sure the terminal is using a UTF-8 encoding to display your data. 只需将适当的字节输出到终端,并确保终端使用UTF-8编码来显示您的数据。 C++ itself is relatively UTF8-agnostic. C ++本身与UTF8无关。 It's just an array of uint_8's. 它只是一个uint_8的数组。

(Unless you want to use some sort of character-oriented operations on strings with UTF-8. Then you need to use UTF-8 manipulation functions, instead of array indexes and the normal string manipulation routines.) (除非你想对UTF-8的字符串使用某种面向字符的操作。然后你需要使用UTF-8操作函数,而不是数组索引和普通的字符串操作例程。)

eg sprintf("%c%c%c\\n", 0xE2, 0x99, 0xA0); 例如sprintf("%c%c%c\\n", 0xE2, 0x99, 0xA0);

Well, you know it is possible because your browser could render them. 嗯,你知道这是可能的,因为你的浏览器可以呈现它们。 On Windows you can use the charmap.exe applet to discover their Unicode code points: 在Windows上,您可以使用charmap.exe小程序来发现它们的Unicode代码点:

  • ♠ = 0x2660 ♠= 0x2660
  • ♣ = 0x2663 ♣= 0x2663
  • ♥ = 0x2665 ♥= 0x2665
  • ♦ = 0x2666 ♦= 0x2666

The challenge is to get a C/C++ program to display them. 挑战是获得一个C / C ++程序来显示它们。 That's not going to be possible in any kind of non-platform specific way unless you use a cross-platform UI library like Qt or wxWidgets. 除非您使用像Qt或wxWidgets这样的跨平台UI库,否则在任何非平台特定方式下都不可能实现这一点。 In a Windows GUI program you can do it like this in the WM_PAINT message handler: 在Windows GUI程序中,您可以在WM_PAINT消息处理程序中执行此操作:

  case WM_PAINT: {
      hdc = BeginPaint(hWnd, &ps);
      HFONT hFont = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Arial Unicode MS");
      HGDIOBJ oldFont = SelectObject(hdc, hFont);
      RECT rc = {0, 0, 666, 16};
      DrawTextEx(hdc, L"\x2660\x2663\x2665\x2666", -1, &rc, DT_LEFT, 0);
      SelectObject(hdc, oldFont);
      DeleteObject(hFont);
      EndPaint(hWnd, &ps);
    }
    break;

In C++: std::wcout << L"wstr [" << wstr << L']' << std::endl; 在C ++中:std :: wcout << L“wstr [”<< wstr << L']'<< std :: endl;

In C: printf("%ls\\n\\n",wstr); 在C:printf(“%ls \\ n \\ n”,wstr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM