Stevie
Stevie

Reputation: 53

print ASCII char on the console in C#

I need to make the console to print ASCII char with 22 chars per "page". With an input, say "key", they will print the next 22 ASCII chars, and so on. The problem is with the "page turning" issue.

Here is my code:

    static void Main(string[] args)
    {

        int i = 0;

        while (i <= 22)
        {
            Console.Write(i + " = " + (char)i);

            if (i < 22)
            {
                Console.Write((char)10);
            }

            i++;
        }

        Console.Write("Please press any key to turn page");

        Console.ReadKey();

        while (i > 22 && i <= 44)
        {
            Console.Write(i + " = " + (char)i);

            if (i < 44)
            {
                Console.Write((char)10);
            }

            i++;
        }

        Console.Write("Please press any key to turn page");

        Console.ReadKey();
    }

I am essentially a newbie. I learn most things by myself, so if I am academically unbearable, please bear me, and just show me how it is done. I can get by from there. Thanks in advance.

Upvotes: 3

Views: 30874

Answers (2)

Elshan
Elshan

Reputation: 7683

If you are not use System.Text.Encoding.GetEncoding(28591); console will give different symbols or incorrect type face for some ASCII chars. More info about GetEncoding(28591)

/*internal const int ISO_8859_1 = 28591;// Latin1;*/

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

Edited: For better format I edited the source with this.

 Console.Write(" "+(char)i );
   if (i % 16 == 0) { // 16*16 = 256
     Console.Write("\n");
 }

Upvotes: 5

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

As far as I can understand, you're trying to print all ASCII table by portions of 22 characters.

This basically can be done by this code snippet:

for (int i = 1; i < 256; i++)
{
    Console.WriteLine(i + " = " + (char)i);

    if (i % 22 == 0)
    {
        Console.WriteLine("Please press any key to turn page");
        Console.ReadKey();
        Console.Clear();
    }
}

Here we're iterating over all 255 characters in ASCII table, writing them line-by-line.

After each character printed out we're checking if it's 22'th characher counted (i % 22 means "remainder from i divided to 22" - thus it will be 0 on 22, 44, 66 and so on).

In the case when it is 22, 44, 66 and so on character - we're printing our "press any key", reading the input and then clearing the screen.

That's it.

Upvotes: 3

Related Questions