Console.Write() - display extended ascii chars?

牧云@^-^@ 提交于 2019-12-01 12:00:03

Console mode apps are restricted to an 8-bit code page encoding. The default on many machines is IBM437, the code page that matches the old IBM PC character set. You can change the code page by assigning the OutputEncoding property:

        Console.OutputEncoding = Encoding.UTF8;

But now you typically have a problem with the font. Consoles default to the Terminal font, an old device font that had glyphs in the right place to produce the IBM PC character set. There are not a lot of fonts available that can produce the proper glyphs that match the Unicode codepoints. Consolas is about it, available on Vista and Win7.

But that's not what you are asking, I think, I'm guessing that you are actually asking about the old box drawing characters. That works without any tinkering with the console settings, you just have to use the right Unicode characters. Here's an example that ought to survive a copy-and-paste:

class Program {
    static void Main(string[] args) {
        Console.WriteLine("╒════════╕");
        Console.WriteLine("│ Hello  │");
        Console.WriteLine("│ world  │");
        Console.WriteLine("╘════════╛");
        Console.ReadLine();
    }
}

To find these characters, use the Windows charmap.exe applet. Click the "Advanced view" checkbox and type "box" in the "Search for" text box, the grid will fill with the box drawing characters. The first usable one that will properly convert to the console is '\u250c'.

The problem appears to be with the Console application rather than with your program. The standard console in windows (cmd.exe) appears not to support Unicode properly - for example, try copying the string below and pasting directly into a cmd.exe window:

Fußball Ö ü

PowerShell seems to suffer from the same problem as well.

One possible solution to your problem is to create a dedicated window/form to be used as an "output console" instead of using the actual console through which the application was executed.

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