11. The character set

The letters, digits, punctuation marks and so on that can appear in strings are called characters, and they make up the alphabet, or character set, that the ZX81 uses. Most of these characters are single symbols, but there are some more, called tokens, that represent whole words, such as PRINT, STOP, **, and so on.

There are 256 characters altogether, and each one has a code between 0 & 255. There is a complete list of them in appendix A. To convert between codes and characters, there are two functions, CODE and CHR$.

CODE is applied to a string, and gives the code of the first character in the string (or 0 if the string is empty).

CHR$ is applied to a number, and gives the single character string whose code is that number.

This program prints out the entire character set.

10 LET A=0
20 PRINT CHR$ A;
30 LET A=A+1
40 IF A<256 THEN GOTO 20

At the top you can see the symbols ", £, $ and so on up to Z, which all appear on the keyboard and can be typed in when you have the  cursor. Further on, you can see the same characters, but in white on black (inverse video); these are also obtainable from the keyboard. If you press GRAPHICS (shifted 9) then the cursor will come up as : this means graphics mode. If you type in a symbol it will appear in its inverse video form, and this will go on until you press GRAPHICS again or NEWLINE. RUBOUT will have its usual meaning. Be careful not to lose the cursor  amongst all the inverse video characters you've just typed in.

When you've experimented a bit, you should still have the character set at the top; if not, then run the program again. Right at the beginning are space and ten patterns of black, white and grey blobs; further on there are eleven more. These are called the graphics symbols and are used for drawing pictures. You can enter these from the keyboard, using graphics mode (except for space, which is an ordinary symbol using the  cursor; the black square is inverse space). You use the 20 keys that have graphics symbols written on them. For instance, suppose you want the symbol , which is on the T key. Press GRAPHICS to get the  cursor, & then press shifted T. From the previous description of the graphics mode, you would expect to get an inverse video symbol; but shifted T is normally <>, a token, and tokens have no inverses: so you get the graphics symbol  instead.

Here are the 22 graphics symbols.

Symbol Code How obtained   Symbol Code How obtained
0 or space   128 SPACE
1 shifted 1   129 shifted Q
2 shifted 2   130 shifted W
3 shifted 7   131 shifted 6
4 shifted 4   132 shifted R
5 shifted 5   133 shifted 8
6 shifted T   134 shifted Y
7 shifted E   135 shifted 3
8 shifted A   136 shifted H
9 shifted D   137 shifted G
10 shifted S   138 shifted F

Now look at the character set again. The tokens stand out quite clearly in two blocks: a small group of three (RND, INKEY$ and PI) after Z, and a larger group (starting with the quote image after , and carrying on from AT up to COPY).

The rest of the characters all seem to be ? This is actually just the way they get printed - the real question mark is between : and (. Of the spurious ones, some are for control characters like , EDIT & NEWLINE, & the rest are for characters that have no special meaning for the ZX81 at all.

Summary

Functions: CODE, CHR$

Exercises

  1. Imagine the space for one key symbol divided up into four quarters: . Then if each quater can be either black or white, there are 2*2*2*2 = 16 possibilities. Find them all in the character set.
  2. Imagine the space for one symbol divided into two horizontally: . Then if each half can be black, white or grey, there are 3*3 = 9 possibilities. Find them all.
  3. The characters in exercise 2 are designed to be used in horizontal bar charts, using two colours, grey and black. Write a program that inputs two numbers A & B (both between 0 & 32), & draws a bar chart for them:


    You will need to start off printing "", and change to either "" or "", according as A is more or less than B.

    What does your program do if A and B are not whole numbers? Or if they are not in the range 0 to 32? A good - 'user friendly' is the fashionable term - program will do something sensible and useful.

  4. There are two different all grey characters on the keyboard, on A & H. If you look at them very close up, you will see that the one on H is like a miniature chessboard, while the one on A is like a sideways chessboard. Print them next to each other, and you will see that they don't join up properly. The one on A is used to join up neatly with  and  (on S & D), while the one on H joins up neatly with  and  (on F and G).
  5. Run this program:

    10 INPUT A

    20 PRINT CHR$A;
    30 GOTO 10

    If you experiment with it, you will find that for CHR$, A is rounded to the nearest whole number; and if A is not in the range 0 to 255 then the program stops with report B.

  6. Using the codes for the characters, we can extend the concept of 'alphabetical ordering' to cover strings containing any characters, not just letters. If instead of thinking in terms of the usual alphabet of 26 letters we use the extended aphabet of 256 characters, in the same order as their codes, then the principle is exactly the same. For instance, these strings are in ZX81 alphabetical order.

"   ZACHARY"
""
"(ASIDE)"
"123 TAXI SERVICE"
"AASVOGEL"
"AA"
"ZACHARY"
"RDVARK"

Here is the rule. First, compare the first characters in the two strings. If these are different, then one of them has its code less than the other, and the string of which it is the first character is the earlier (lesser) of the two strings. If they are the same, then go on to compare the next characters. If in this process one of the strings runs out before the other, then that string is the earlier; otherwise they must be equal.

Type in again the program in exercise 4 of chapter 10 (the one that inputs two strings and prints them in order), and use it to experiment.

  1. This program prints a screenful of random black & white graphics characters:

10 LET A=INT (16*RND)
20 IF A>=8 THEN LET A=A+120
30 PRINT CHR$ A;
40 GOTO 10

(How does it work?)