JSz
JSz

Reputation: 137

How to use box drawing high ASCII characters in Visual Studio 15 console app?

I have a Win32 console app that plays the game of life in a CMD window.

I would like to draw a box around the field using some of the standard box drawing characters such as: (186: ║), (187: ╗) (188: ╝), (200: ╚), (201: ╔), (205: ═) but I'm not getting anywhere.

First, during build I get warnings such as: "warning C4566: character represented by universal-character-name '\u2551' cannot be represented in the current code page (1252)"

The code will run, but it will display something else in place of the box drawing characters. I looked through as many properties as I could find, but I did not find any place to change the current codepage.

Any idea on how and where to change it? Or is this something I need to change in Windows perhaps? I'm running Win 7 Pro, if that matters.

I did change the VS editor's codepage to 850 (Western European DOS) but that had no effect on the executable.

Upvotes: 5

Views: 3502

Answers (1)

Tom Blodget
Tom Blodget

Reputation: 20812

Nota Bene: "high ASCII" and are not specific enough to be meaningful. (Please just stop using those terms.)

Last things first: your console has to have a font selected that supports the encoding and characters that you want to use. This shouldn't be a problem for box-drawing characters since 1981. But you can check by inspecting your console setting and using charmap.

It's best not to mess with the default C# source file encoding. UTF-8 is great.

Next, use the characters you want to use in your source code as literal UTF-16 code units. So, "║" instead of 186—but "\u2551" if you must. It makes the code so much easier to read.

Console.OutputEncodingIt's best to use UTF-8 for your output unless somehow it is not supported or is awkward to use.

If you can, change your console encoding to UTF-8 using the chcp 65001 command. If you can't, go back and change your Console.OutputEncoding to whatever your console is using (codepage 437, for example).


Here's an example:

class Program
{
    static void Main(string[] args)
    {
        var codePage = Console.OutputEncoding.CodePage.ToString();
        var length = codePage.Length;
        Console.WriteLine($"╔{new String('═', length)}╗");
        Console.WriteLine($"║{codePage}║");
        Console.WriteLine($"╚{new String('═', length)}╝");
    }
}

It displays this, after I do chcp 65001 in cmd.exe:

╔═════╗
║65001║
╚═════╝

And this, if I run it without doing chcp first:

╔═══╗
║437║
╚═══╝

And this, after I do chcp 850 in cmd.exe:

╔═══╗
║850║
╚═══╝

Upvotes: 2

Related Questions