COPING WITH CHARACTERS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
You will have noticed that, in addition to the normal typewriter characters there are several other characters not usually found on a typewriter. In addition there are some usable characters not written on the keyboard. Before we go on to investigate what characters can be printed, meet an interesting function: CHR$(X) where X is a number integer variable or expression. It is used mainly in conjunction with the PRINT statement. CHR$(X) means "the character whose code is x" Try this: 10 INPUT X 20 PRINT CHR$(X) 30 GO TO 10 What this program does is to ask for a number and then to print out the character whose code is the number entered. The codes lie between 0 and 255. A useful program for listing the symbols is given below. 10 PRINT "ENTER CODE VALUE" 20 INPUT X 30 PRINT X; ". . .";CHR$(X) 40 PRINT 50 LET X = X + 1 60 GO TO 30 This lists 11 symbols together with their codes. starting with the code entered at the start of the program.
Inverse means that the character appears white on a black background. Inverse space is a black square, for instance. The graphics characters are shown below: CHR$ (X) allows us to print any character we want. There is a further group of facilities which enables us to handle characters; These are (a) TL$ (string) - this gives the string minus its first character. The string can be a literal string inside quotes or a string variable. 10 PRINT TL$ ("ABC") would give BC 10 PRINT TL$(G$) will cut off the first character of string G$ (b) CODE (string) - this gives the code corresponding to the first character in a string (either a string variable or a literal string) 10 PRINT CODE("ABC") would print 38 which is the code for A. An example of the way in which these can be used is the following program which accepts a string and prints it out in inverse video. 10 PRINT "ENTER YOUR STRING" 20 INPUT G$ 30 PRINT G$ 40 LET X = CODE (G$) 50 LET X = X+128 60 IF G$=CHR$ (1) THEN GO TO 100 70 PRINT CHR$ (X); 80 LET G$ = TL$(G$) 90 GO TO 40 100 STOP Statement: 40 sets X = code of the first character of G$. 50 adds 128 to the code (this gives Inverse of the letters, digits and graphics). 60 test the string to see if it is a null string, i.e. has no characters in it. If it is a null string either the input string was a null string to begin with or all the characters have been converted to inverse video and printed. 70 prints the inverse video character. 80 chops off the character which has just been printed and the program then jumps back to 40 and the code for the next character is extracted. The program goes on until the string has been shortened to the null string. There is a further function which can be useful. This is: STR$ (variable or number). This allows an integer number or variable to be treated as a string variable 10 LET G$=STR$( 1234) sets G$= "1234" 10 LET G$=STR$(X) does the same thing for an integer variable — if X = 4852 then G$ = "4852". So far we've not said very much about the graphics symbols. These have been designed so as to double the effective resolution of the display, which gives 23 lines of 32 characters each. Here is a program which plots TWO bar charts on the same display.
The program prints Z as black bars, Y as grey bars. Statements 80 to 130 determine what graphic symbol is to be used; or nothing at all. This program calculates the graphs of Y = X and Z = 24 — X and plots them in bar chart form. As you will see if you run the program it produces a very clear, unambiguous display. You can use the same sort of trick to achieve greater resolution along the line as well as from line to line. Lines 85 to 125 decide what character will be printed, depending on the relative size of J, Z and Y. These examples are only scratching the surface when it comes to character manipulation and graphics. The possibilities are almost literally endless. |