How can I display extended Unicode character in a C# console?

Nigel Colpitts picture Nigel Colpitts · Mar 2, 2013 · Viewed 8.3k times · Source

I'm trying to display a set of playing cards, which have Unicode values in the 1F0A0 to 1F0DF range. Whenever I try to use chars with more than 4 chars in their code, I get errors. Is it possible to use these characters in this context? I'm using Visual Studio 2012.

char AceOfSpades = '\u1F0A0'; immediately upon typing gives me the error "Too many characters in character literal" This still shows up with either of the Unicode or UTF8 encodings. If I try to display '\u1F0A' like above... With Unicode it shows '?' With UTF8 it shows 3 characters.

I tried all the given options for OutputEncoding string AceOfSpades = "\U0001F0A0"; Default, Unicode, ASCII: ?? UTF7: +2DzcoA- UTF8: four wierd characters UTF32 , BigEndianUnicode: IOException Console.OutputEncoding = System.Text.Encoding.UTF32;, despite being an option, crashes even if it's the only line of code. UTF16 was not on the list.

How can I check which version of Unicode I'm using?

Answer

R. Martinho Fernandes picture R. Martinho Fernandes · Mar 2, 2013

In order to use characters from outside the Basic Multilingual Plane, you need to escape them with \U, not \u. That requires eight hexadecimal digits (which, to be honest, makes little sense, since all Unicode characters can be written with six hexadecimal digits).

However, the type char in .NET can only represent UTF-16 code units, meaning that characters outside the BMP require a pair of chars (a surrogate pair). So you have to make it a string.

string AceOfSpades = "\U0001F0A0";