0

I am writing a .NET 5 console application in C#. One of the things I would like to be able to do is to display two specific Unicode characters: a full heart ( - Unicode number 2665) and an empty heart ( - Unicode number 2661). Thus:

const char emptyHeart = '\u2661';
const char fullHeart = '\u2665';

I assume I have that set properly, but for some reason the console is showing the emptyHeart as a question mark instead of the proper character. Thus, for example, a string containing three fullHearts followed by four emptyHearts shows as ♥♥♥???? instead of the intended ♥♥♥♡♡♡♡. Is this fixable without changing the Unicode characters (such as to filled and empty circles)? If so, how?

Tyll'a
  • 526
  • 1
  • 5
  • 17
  • I found another similar question on SO that may be relevant to your issue: https://stackoverflow.com/a/9100267/1188197. Yours is C# not Java, but may still lead you towards a solution. – EspressoBeans Aug 11 '21 at 07:03
  • Does this answer your question? [How to write Unicode characters to the console?](https://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console) – l33t Aug 11 '21 at 07:05
  • Yes, I looked at both of those, and no, it doesn't answer my question. Unfortunately. – Tyll'a Aug 11 '21 at 07:54

1 Answers1

1

https://learn.microsoft.com/dotnet/api/system.console.outputencoding?view=net-5.0#notes-to-callers

Note that successfully displaying Unicode characters to the console requires the following:

  • The console must use a TrueType font, such as Lucida Console or Consolas, to display characters.
  • A font used by the console must define the particular glyph or glyphs to be displayed. The console can take advantage of font linking to display glyphs from linked fonts if the base font does not contain a definition for that glyph.

The default Windows console font, Consolas, does include a glyph for \u2665, but not for \u2661 as per the U+2660 row from https://www.fileformat.info/info/unicode/font/consolas/grid.htm.

Your only option, therefore, is to change the console's font to one that does support that glyph. However this is not a trivial operation; see "The following example shows how you can programmatically change the font from a raster font to Lucida Console" from https://learn.microsoft.com/dotnet/api/system.console#unicode-support-for-the-console (although Lucida Console doesn't support \u2661 either, so you'll have to find a font that does).

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138