ASCII Character Codes

ASCII refers to character codes from zero through 127. These codes are interpreted identically on all modern computers. They represent basic alphabetic, numeric, and symbol characters.

Of course, ASCII is an acronym. It stands for American Standard Code for Information Interchange. It’s pronounced ask-ee, which doesn’t rhyme with blorf.

The ASCII code established a standard for common computer characters. In fact, early microcomputers (in the 1970s and early 1980s) were advertised as “ASCII compatible,” which would seem odd today. That’s because back then each system used a different coding scheme for its text. Simple text, such as Mary had a little lamb, couldn’t be sent from one computer to another. Often times a translation program was required — yes, even for text.

Due to memory limitations, the ASCII code defines only 128 characters, which is half the storage in an 8-bit byte (256 characters). That limitation eventually lead to the establishment of the Unicode standard, which defines zillions of characters and is widely used today. Still, the first 128 characters in Unicode are the basic ASCII characters.

The ASCII codes are divided into chunks:

Codes 0 through 31 are control characters. For example, code 13 is Control-M, also written as ^M. It represents the Enter key on a PC. Code 8 is Ctrl+H or Backspace. On some terminals you can press Ctrl+H to backspace.

Codes 32 through 126 are printable characters, although Code 32 is the space character, which doesn’t print.

Code 127 is the Delete code, which may backspace and erase on some terminals, or display a character on others.

The following C language source code can be compiled to display ASCII codes and characters. Although you can see a full, detailed chart by visiting the ASCII Table web site.

#include <stdio.h>

int main()
{
    int ascii;

    puts("ASCII Chart");
    printf(" Dec Hex Code\t");
    printf(" Dec Hex Code\t");
    printf(" Dec Hex Code\t");
    printf(" Dec Hex Code\n");

    for(ascii=0;ascii<32;ascii++)
    {
        printf(" %3d  %02X  ^%c\t %3d  %2X   %c\t\
%3d  %2X   %c\t %3d  %2X   %c\n",
                ascii,
                ascii,
                ascii+'@',
                ascii+32,
                ascii+32,
                ascii+' ',
                ascii+64,
                ascii+64,
                ascii+'@',
                ascii+96,
                ascii+96,
                ascii+'`');
    }

    return(0);
}

Don’t let the split printf() statement at Lines 15 and 16 throw you. That’s all one statement, just divided between two lines. Also, each variable for printf() has been separated out on a line by itself. If I didn’t do this then that single printf() statement would be long and ungainly.

Here’s the output:

ASCII Chart
 Dec Hex Code	 Dec Hex Code	 Dec Hex Code	 Dec Hex Code
   0  00  ^@	  32  20    	 64  40   @	  96  60   `
   1  01  ^A	  33  21   !	 65  41   A	  97  61   a
   2  02  ^B	  34  22   "	 66  42   B	  98  62   b
   3  03  ^C	  35  23   #	 67  43   C	  99  63   c
   4  04  ^D	  36  24   $	 68  44   D	 100  64   d
   5  05  ^E	  37  25   %	 69  45   E	 101  65   e
   6  06  ^F	  38  26   &	 70  46   F	 102  66   f
   7  07  ^G	  39  27   '	 71  47   G	 103  67   g
   8  08  ^H	  40  28   (	 72  48   H	 104  68   h
   9  09  ^I	  41  29   )	 73  49   I	 105  69   i
  10  0A  ^J	  42  2A   *	 74  4A   J	 106  6A   j
  11  0B  ^K	  43  2B   +	 75  4B   K	 107  6B   k
  12  0C  ^L	  44  2C   ,	 76  4C   L	 108  6C   l
  13  0D  ^M	  45  2D   -	 77  4D   M	 109  6D   m
  14  0E  ^N	  46  2E   .	 78  4E   N	 110  6E   n
  15  0F  ^O	  47  2F   /	 79  4F   O	 111  6F   o
  16  10  ^P	  48  30   0	 80  50   P	 112  70   p
  17  11  ^Q	  49  31   1	 81  51   Q	 113  71   q
  18  12  ^R	  50  32   2	 82  52   R	 114  72   r
  19  13  ^S	  51  33   3	 83  53   S	 115  73   s
  20  14  ^T	  52  34   4	 84  54   T	 116  74   t
  21  15  ^U	  53  35   5	 85  55   U	 117  75   u
  22  16  ^V	  54  36   6	 86  56   V	 118  76   v
  23  17  ^W	  55  37   7	 87  57   W	 119  77   w
  24  18  ^X	  56  38   8	 88  58   X	 120  78   x
  25  19  ^Y	  57  39   9	 89  59   Y	 121  79   y
  26  1A  ^Z	  58  3A   :	 90  5A   Z	 122  7A   z
  27  1B  ^[	  59  3B   ;	 91  5B   [	 123  7B   {
  28  1C  ^\	  60  3C   <	 92  5C   \	 124  7C   |
  29  1D  ^]	  61  3D   =	 93  5D   ]	 125  7D   }
  30  1E  ^^	  62  3E   >	 94  5E   ^	 126  7E   ~
  31  1F  ^_	  63  3F   ?	 95  5F   _	 127  7F   

Thanks to the geniuses who developed the ASCII codes, you can perform some clever and useful programming tricks by using these code values. I’ll show you one of those tricks next Lesson.