Home: WWW Information: Publishing Web Docs: ASCII Characters:

ASCII Character Usage

The following is from the CS102 Course at the Univ. of Dundee  Scotland.

The first 32 ASCII codes (000 0000 to 001 1111 or 0 to 31 in decimal) are
reserved for special codes, most of which are invisible if printed on a
terminal screen. They are also interpreted differently by some programs
(such as text editors). For example, 13 is a carriage return, which
positions the cursor at the beginning of a line, 10 is a line feed, which
moves the cursor down one line and so on. On many keyboards many of these
codes can be produced by holding down the control key and pressing one of
the alphabet keys A to Z. You should be careful in trying this, though,
since some codes have special meanings to certain terminals and can cause
them to do strange things!

For example, in the text editor vi, Control-F moves the cursor 1 full screen
forward and Control-B moves it one screen back. In another text editor,
emacs (or mg), Control-F and Control-B move the cursor one character forward
and back respectively.

In ordinary operation, Control-S will freeze output on the screen, so that
if a screen printout is scrolling off the top, you can stop it. Control-Q
unfreezes the screen and permits scrolling to continue.

Type "stty -a" on UNIX to see how Control characters are used in processing
what you type.

ASCII 32 to 47 are punctuation codes and other special symbols like $%and so
on. ASCII 48 to 57 are codes for the digits 0 to 9. Note that the binary
code for the numbers is 011 0000 through 011 1001, i.e. the lower nybble is
a base 2 number equal to the digit character it represents. ASCII 58 to 64
are a few more punctuation symbols. ASCII 65 to 90 code for the uppercase
letters A through Z in order. The binary codes for the letters are 100 0001
through 101 1010. The 5 least significant bits cover the decimal numbers 1
through 26. ASCII 91 through 96 are more punctuation symbols. ASCII 97
through 122 are the lower case letters. Again, the 5 least sig bits are base
2 1 through 26. Finally, ASCII 123 through 126 are punctuation and ASCII 127
is the code for the DELETE key.

In Pascal, conversion between the ASCII integer code for a character and the
character itself is done with the ord() and chr() built-in functions. ord()
takes a character as its argument and returns the ASCII code for it, so that
ord('A') would return 65. Similarly, chr() takes an integer and returns the
corresponding character, so that chr(66) would return 'B'. chr() can also be
used to `print out' the control codes, though you should be careful in doing
this from within a program since they can seriously botch up a terminal. One
code that is reasonably safe though is code 7, which causes the terminal
bell to ring.

Using these two functions, it is possible to write programs, useful in text
editors and word processors for example, which change the case of letters of
the alphabet. The following program, for example, changes all lowercase to
uppercase letters in a short string:

program change_case(input,output);

const
  maxstring = 25;  {Maximum string size}
  lower_a = 97;    {Locations of ends of alphabet}
  lower_z = 122;
  lower2upper = 32;  {Conversion from lower to upper case}

type
  sentence = packed array[1..maxstring] of char;

var
  words : sentence;

{******************************************}
procedure switch(var s : sentence);
var
  i : integer;
  asc : integer; {ASCII code}

begin
  for i := 1 to maxstring do begin
    asc := ord(s[i]);
    if (asc >= lower_a) and (asc <= lower_z) then
      s[i] := chr(asc - lower2upper);
  end; {for i}
end; {switch}
{******************************************}

{main}
begin
  write('Enter string to be modified: ');
  readln(words);
  switch(words);
  writeln('New string: ',words);
end.

The program searches for all occurrences of characters with ASCII codes in
the range   , since these are the ASCII codes for `a' and `z'. It then
subtracts 32 from these codes, which converts them into ASCII codes for the
corresponding uppercase letters, and converts the result back to a character
using the chr() function.

SUMMARY

   * Data stored as bits.
   * 8 bits = 1 byte.
   * Convert decimal to binary by successive division by 2.
   * Binary to decimal: multiply bit by place value (power of 2).
   * Octal is base 8 (uses digits 0 to 7).
   * Hex is base 16 (uses 0 to 9, A to F).
   * Convert from decimal to octal (hex) by division by 8 (16).
   * Convert from octal (hex) to binary by replacing each digit by 3 (4)
     bits.
   * Convert between octal and hex by going via binary.
   * ASCII code used for characters.
        o Codes 0 to 31 for non-printable characters.
        o Codes 48 to 57 for digits 0 to 9.
        o Codes 65 to 90 for letters A to Z.
        o Codes 97 to 122 for letters a to z.
        o Other codes for punctuation.

----------------------------------------------------------------------------

growe@mcs.dundee.ac.uk