Print ASCII line art characters in C# console application

I would like to have a C# console application print the extended ASCII Codes from http://www.asciitable.com/. In particular I am looking at the line art characters: 169, 170, 179-218. Unfortunately when I tried, I ended up getting 'Ú' for 218 and expect to see the other characters from http://www.csharp411.com/ascii-table/.

I'm aware that ASCII only specifies character codes 0 - 127. I found another post with a reference to SetConsoleOutputCP(), but was not able to get that to work in a C# class or find an example of how to do so.

Is it possible to print the line art characters in a C# console application? If it is can someone provide a URL to an example or the code?


Solution 1:

A small program that modifies the codepage used by the Console.OutputEncoding property to use the characters you desire:

class Program
{
    static void Main(string[] args)
    {
        Console.OutputEncoding = System.Text.Encoding.GetEncoding(1252);
        Console.WriteLine((char) 169);
        Console.WriteLine((char) 170);

        for(char c = (char)179; c <= (char)218; ++c)
        {
            Console.WriteLine(c);
        }
    }
}

EDIT:

So I went ahead and looked up the Unicode equivalents of the box art. There's a few extra glyphs that may be useful to you. That Wikipedia page lists all of their code points.

I've put together this to try them out:

class Program
{
    static void Main(string[] args)
    {
        for(int i = 0x2500; i <= 0x2570; i += 0x10)
        {
            for(int c = 0; c <= 0xF; ++c)
            {
                Console.Write((char) (i + c));
            }

            Console.WriteLine();
        }
    }
}

For me, quite a few glyphs simply come up as ?, but the standard box-art glyphs we're used to seeing in the old ASCII games do appear for me. Hopefully these will work for you.

Solution 2:

To view correct ascii's on console I just do this:

Console.OutputEncoding = System.Text.Encoding.GetEncoding(28591);

To understand ASCII Chart I wrote below code:

using System;

namespace AsciiChart
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.GetEncoding(28591);
            for (int i = 0; i < 256; i++) {
               Console.Write(i+"=> ["+(char)i +"]  \n");
            }
            Console.ReadKey();
        }
    }
}

enter image description here

Let's begin to draw, sample 1:

using System;

namespace AsciiBorder
{
    class Program
    {
        static void Main(string[] args)
        {
            int topleft = 218;
            int hline = 196;
            int topright = 191;
            int vline = 179;
            int bottomleft = 192;
            int bottomright = 217;

            Console.OutputEncoding = System.Text.Encoding.GetEncoding(28591);
            //draw top left corner
            Write(topleft);
            //draw top horizontal line
            for (int i = 0; i < 10; i++)
                Write(hline);
            //draw top right corner
            Write(topright);
            Console.WriteLine();
            //draw left and right vertical lines
            for (int i = 0; i < 6; i++)
            {
                Write(vline);
                for (int k = 0; k < 10; k++) {
                    Console.Write(" ");
                }
                WriteLine(vline);
            }
            //draw bottom left coner
            Write(bottomleft);
            //draw bottom horizontal line
            for (int i = 0; i < 10; i++)
                Write(hline);
            //draw bottom right coner
            Write(bottomright);
            Console.ReadKey();
        }
        static void Write(int charcode)
        {
            Console.Write((char)charcode);
        }
        static void WriteLine(int charcode)
        {
            Console.WriteLine((char)charcode);
        }
    }
}

Console Output:

enter image description here

More info about Latin 1 & ASCII Code Pages

Completed 3x3 Tic-Tac-Toe like board drawing code: Sample 2

enter image description here

Code:

using System;

namespace AsciiBorder
{
    class Program
    {
        const int topleft = 218, hline = 196, topright = 191, vline = 179, bottomleft = 192, bottomright = 217, cross = 197, topT = 194, bottomT = 193, leftT = 195, rightT = 180;
        const int space = 10/*this determine size of the single cell*/, spacer_ex = (space / 2) + 1;
        static void Main(string[] args)
        {            
            Console.OutputEncoding = System.Text.Encoding.GetEncoding(28591);
            Console.Title = "3x3 Board";
            DrawTop();
            DrawMidSpacer();
            DrawMiddle();
            DrawMidSpacer();
            DrawMiddle();
            DrawMidSpacer();
            DrawBottom();

            Console.ReadKey();
        }
        static void DrawBottom() {
            #region bottom
            Write(bottomleft);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(bottomT);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(bottomT);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(bottomright);

            Console.WriteLine();
            #endregion
        }
        static void DrawMiddle() {
            #region middle
            Write(leftT);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(cross);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(cross);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(rightT);

            Console.WriteLine();
            #endregion
        }
        static void DrawMidSpacer() {
            #region middlespacer
            for (int x = 0; x < spacer_ex; x++)
            {
                Write(vline);
                for (int i = 0; i < space; i++)
                    Console.Write(" ");
                Write(vline);
                for (int i = 0; i < space; i++)
                    Console.Write(" ");
                Write(vline);
                for (int i = 0; i < space; i++)
                    Console.Write(" ");
                Write(vline);

                Console.WriteLine();
            }
            #endregion
        }
        static void DrawTop() {
            #region top
            Write(topleft);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(topT);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(topT);
            for (int i = 0; i < space; i++)
                Write(hline);
            Write(topright);

            Console.WriteLine();
            #endregion
        }
        static void Write(int charcode)
        {
            Console.Write((char)charcode);
        }
        static void WriteLine(int charcode)
        {
            Console.WriteLine((char)charcode);
        }
    }
}

Solution 3:

You can simply use Signs from the ASCII-table in windows.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("╔═╗");
        Console.WriteLine("╚═╝");
    }
}

Solution 4:

I battled this for days a while back. I don't think it can be done, regardless of what other people say. Now, I was trying to make a Dwarf Fortress style game. If you are doing the same, do what he did. Use images.

  • Faster, because it can make use of graphic acceleration.
  • Easier, because they are tiles and there are LOTS of tutorials on doing that.
  • Well Supported, with things like XNA for the very framework you are already using.
  • Extensible, so you can swap in other images at a later date, for new images like the bearded smiles in DF.