简体   繁体   中英

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

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 = '\Ἂ0'; 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 '\Ἂ' 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?

In order to use characters from outside the Basic Multilingual Plane, you need to escape them with \\U , not \\u\u003c/code> . 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 char s (a surrogate pair). So you have to make it a string.

string AceOfSpades = "\U0001F0A0";

I am going to assume (until you edit your post for clarity) that your symbols are not displaying properly. If this is not the case, I will delete this answer.

Set your console's encoding to Unicode or UTF-8.

Console.OutputEncoding = System.Text.Encoding.Unicode

or

Console.OutputEncoding = System.Text.Encoding.UTF8 .

Make sure the font can display Unicode/UTF-8 characters (like Lucida Console).

I am displaying 10 Egyptian hieroglyphs from the Extended Unicode in a Windows application (not console) like that:

string single_character = "\U00013000";//first ancient hieroglyph

//get the Unicode index
Encoding enc = new UTF32Encoding(false, true, true);
byte[] b = enc.GetBytes(single_character);
Int32 code = BitConverter.ToInt32(b, 0);

for (int i = 0; i < 10; i++)
{
   //convert from int Unicode index to display character
   string glyph = Char.ConvertFromUtf32(code); //single one

   textBox1.Text += glyph;

   code++;
 }

You also need a font that supports these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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