简体   繁体   中英

C# console + Unicode

First of all, Console.OutputEncoding doesn't help. So i go on unicode table site , choose some symbols and try to output something. But only ?? is produced. Where am I wrong?

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main()
        {
            Console.OutputEncoding = Console.InputEncoding = Encoding.Unicode;
            string s = "\u2654\u2657";
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
}

The default font in the console window is not a Unicode font. You have to change it to a Unicode font like Lucida Console or Consolas so that it can display characters outside the regular 8-bit codepage.

Ref: http://msdn.microsoft.com/en-us/library/system.console.outputencoding%28v=vs.110%29.aspx

Under Remarks:

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.

在@Guffa中说过,尝试在http://www.compileonline.com/compile_csharp_online.php找到代码是可行的,因为@Guffa表示请确保您的控制台编码能够解码提到的字符。

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