How I print UTF-8 characters C++?

10,296

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:

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

The challenge is to get a C/C++ program to display them. 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. In a Windows GUI program you can do it like this in the WM_PAINT message handler:

  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;
Share:
10,296
y2k
Author by

y2k

Updated on June 19, 2022

Comments