Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print ASCII line art characters in C# console application

Tags:

c#

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?

like image 256
Suirtimed Avatar asked Dec 22 '10 16:12

Suirtimed


People also ask

How we can print ASCII value in c?

Try this: char c = 'a'; // or whatever your character is printf("%c %d", c, c); The %c is the format string for a single character, and %d for a digit/integer. By casting the char to an integer, you'll get the ascii value.

How do I print ASCII to string?

#include <stdio. h> #include <string. h> int main() { int e; char name[100] = ""; // Allow for up to 100 characters printf("Enter a name : \n"); // scanf("%c", &name); // %c reads a single character scanf("%99s", name); // Use %s to read a string!

How do I print an ASCII letter?

Then, how to print character? We can use cast type here, by casting into char we are able to get result in character format. We can use cout<<char(65) or cout<<char(var), that will print 'A'. (65 is the ASCII value of 'A').


1 Answers

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.

like image 120
Joshua Rodgers Avatar answered Nov 15 '22 23:11

Joshua Rodgers