Search code examples
c#consoleascii

C# Console is outputting box characters as "?"


I am making a game in ascii for fun and I have an issue where the characters are not printed in the way that i have expected them. instead of the characters being printed normally, it instead prints out question marks. here's the situation:

Expected Outcome:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ PacGunMan         [_][^][X] ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃          Main Menu          ┃
┃    ━━━━━━━━━━━━━━━━━━━━━    ┃
┃                             ┃
┃          [1] Start          ┃
┃         [2] Settings        ┃
┃         [3] Credits         ┃
┃           [4] Exit          ┃
┃                             ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

What I actually got:

???????????????????????????????
? PacGunMan         [_][^][X] ?
???????????????????????????????
?          Main Menu          ?
?    ?????????????????????    ?
?                             ?
?          [1] Start          ?
?         [2] Settings        ?
?         [3] Credits         ?
?           [4] Exit          ?
?                             ?
???????????????????????????????

Rendering Source Code:

public static void Automatic(List<string> RenderItem) {
    Console.WriteLine("Rendering...");
    foreach (string Y in RenderItem) {   // The variable is named "Y" because Y-Axis
        Console.WriteLine(Y);
    }
}

Solution

  • C#, for some reason, prints characters in ASCII Encoding. So we need to change the encoding type to UTF-8.

    One of the reasons why this happens is maybe because the characters may not actually be in an ASCII format. But in this case, it was the printing error/problem and not the characters' problem.

    So here's how we can do that:

    Console.OutputEncoding = Encoding.UTF8; // Get C# to do this before outputting/printing.
    

    so here's how the Rendering Source Code should look like:

    public static void Automatic(List<string> RenderItem) {
        Console.OutputEncoding = Encoding.UTF8;
        Console.WriteLine("Rendering...");
        foreach (string Y in RenderItem) {
            Console.WriteLine(Y);
        }
    }