Outputting a Unicode character in C#

江枫思渺然 提交于 2019-12-17 20:24:35

问题


I'm new to programming and self taught. I'm trying to output the astrological symbol for Taurus, which is supposed to be U+2649 in Unicode. Here is the code I'm using...

string myString = "\u2649";
byte[] unicode = System.Text.Encoding.Unicode.GetBytes(myString);
Console.WriteLine(unicode.Length);

The result I'm getting is the number 2 instead of the symbol or font. I'm sure I'm doing something wrong.


回答1:


You need to have a font which displays that glyph. If you do, then:

Console.WriteLine(myString); 

is all you need.

EDIT: Note, the only font I could find which has this glyph is "MS Reference Sans Serif".




回答2:


Why are you converting it to unicode, this will not do anything.. lose the conversion and do the following:

string a ="\u2649"  ; 

Console.write(a) ; 



回答3:


The length of the Unicode character, in bytes, is 2 and you are writing the Length to the Console.

Console.WriteLine(unicode.Length);

If you want to display the actual character, then you want:

Console.WriteLine(myString); 

You must be using a font that has that Unicode range for it to display properly.

UPDATE:

Using default console font the above Console.WriteLine(myString) will output a ? character as there is no \u2649. As far I have so far googled, there is no easy way to make the console display Unicode characters that are not already part of the system code pages or the font you choose for the console.

It may be possible to change the font used by the console: Changing Console Fonts




回答4:


You are outputting the length of the character, in bytes. The Console doesn't support unicode output, however, so it will come out as an '?' character.



来源:https://stackoverflow.com/questions/3162116/outputting-a-unicode-character-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!