Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2014
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace PrintTheASCIITable
  5. {
  6.     class ASCIITable
  7.     {
  8.         static void Main()
  9.         {
  10.             Console.Write("Decimal".PadRight(10));
  11.             Console.Write("ASCII".PadRight(10));
  12.             Console.WriteLine("Hex".PadRight(10));
  13.             Console.WriteLine();
  14.  
  15.             byte min = 0;
  16.             byte max = 128;
  17.  
  18.             string display;
  19.             char c;
  20.  
  21.             for (byte i = min; i < max; i++)
  22.             {
  23.                 // Get ascii character.
  24.                 c = Encoding.ASCII.GetChars(new byte[] { i })[0];
  25.  
  26.                 // Get display string.
  27.                 display = string.Empty;
  28.                 if (char.IsWhiteSpace(c))
  29.                 {
  30.                     switch (c)
  31.                     {
  32.                         case '\t':
  33.                             display = "\\t";
  34.                             break;
  35.                         case '\n':
  36.                             display = "\\n";
  37.                             break;
  38.                         case ' ':
  39.                             display = "\' \'";
  40.                             break;
  41.                         case '\r':
  42.                             display = "\\r";
  43.                             break;
  44.                         case '\v':
  45.                             display = "\\v";
  46.                             break;
  47.                         case '\f':
  48.                             display = "\\f";
  49.                             break;
  50.                     }
  51.                 }
  52.                 else if (char.IsControl(c))
  53.                 {
  54.                     display = "control";
  55.                 }
  56.                 else
  57.                 {
  58.                     display = "\'" + c.ToString() + "\'";
  59.                 }
  60.                 // Write table row.
  61.                 Console.Write("{0}       ", i.ToString().PadLeft(3));
  62.                 Console.Write(display.PadRight(10));
  63.                 Console.WriteLine(i.ToString("X2"));
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement