Console.Write() - display extended ascii chars?

后端 未结 2 801
-上瘾入骨i
-上瘾入骨i 2021-01-13 08:35

I am able to correctly display the standard ASCII symbols (up to 127) like \"heart\", \"note\" you know what I mean. I would like to also display ones that I can use for dra

2条回答
  •  别那么骄傲
    2021-01-13 09:12

    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'.

提交回复
热议问题