Thread: ASCII Printable Characters

  1. #1
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135

    ASCII Printable Characters

    Does anyone know how to print the characters that correspond to the ASCII codes from 0-255 in C#?
    Code:
    for (int i = 0; i < 256; ++i)
    {
    	Console.WriteLine("The value for " + i + " is: " + (char)i);
    }
    This doesn't work I presume because the char represents UNICODE characters.

  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187

    something like this ?

    Code:
    using System;
    namespace PrintChars
    {
    	class Class1
    	{
    		[STAThread]
    		static void Main(string[] args)
    		{
    			for(int i=0; i<=255; i++)
    			{
    				Console.WriteLine(i.ToString() + "\t=\t" + Convert.ToChar(i));
    			}
    			Console.Read();
    		}
    	}
    }

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that's about the same as casting.
    I think the only really correct way to go about it is to use the System.Text.ASCIIEncoding class.
    Code:
    byte[] allAscii = new byte[128];
    for(int i=0;i<128;++i)
      allAscii[i] = i;
    
    char[] showAscii = new System.Text.ASCIIEncoding().GetChars(allAscii);
    for(int i=0;i<128;++i)
      System.Console.Write(showAscii[i]);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    D'oh! Nearly, but not quite. I am interested in those characters belonging to the extended ASCII set (should have specified that I guess ) in the range of 180 to 210.

    I have tried adapting the code, but it looks as though this class only deals with the standard ASCII set..

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Replies: 5
    Last Post: 12-22-2004, 05:46 PM
  4. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM