CHR$

BASin

CHR$ CHaRacter string

Function

The characters and keywords available on the keyboard plus any user-defined graphics make up the BASIC character set. By using CHR$ and a code number, each one can be obtained as a string. The character set also contains several display codes that affect the display of characters. These codes can be brought into operation on characters displayed by using PRINT before CHR$. The complete character set can be found here.

How to use CHR$

CHR$ is followed by a numeric value, for example

80 PRINT CHR$ x

An expression must be enclosed by brackets. The value following CHR$ (x above) is rounded to the nearest integer. If it is in the range 32 to 255, CHR$ returns the a keyboard character, user-defined graphics character or a keyword as a string. The BASIC uses the ASCII code for values from 32 to 95 and 97 to 126. If x is assigned a value of 65, the above statement displays A, for example.

CHR$ control codes

Values from 1 to 31 either return control codes or are not used. CHR$ 6 (PRINT comma), 8 (back space) and 13 (new line or ENTER) affect displays on the screen if inluded in a PRINT statement. CHR$ may be followed by the code value and a semicolon, for example

60 PRINT "A";CHR$ 6;"B"

This statement displays

A               B

Another way of using CHR$ control codes is to form a composite string containing them. The statement

60 PRINT "A"+CHR$ 6+"B"

has exactly the same effect as the previous example.

Codes 16 to 23 affect colour and position and each may be used in a composite string together with CHR$ followed by a colour code value from 0 to 7 for CHR$ 16 (INK control) and CHR$ 17 (PAPER), or by 0 or 1 for CHR$ 18 to CHR$ 21 (FLASH, BRIGHT, INVERSE, and OVER controls). The command

PRINT CHR$ 16+CHR$ 3+CHR$ 17+CHR$ 6+CHR$ 18+CHR$ 1+"BASin"

displays BASin in flashing red and yellow. Alternatively, as above, each plus (+) sign may be replaced by a semicolon.

CHR$ 22 (AT control) is followed by two CHR$ values to indicate the line and column numbers. The command

PRINT CHR$ 22+CHR$ 11+CHR$ 16+CHR$ 42

displays a star in the centre of the screen.

CHR$ 23 (TAB control) is also followed by two values in the same way. The second value is normally 0 and the first gives the TAB position. The command

PRINT CHR$ 23+CHR$ 16+CHR$ 0+CHR$ 42

displays a star halfway across the screen.

Note that only these controls are available. Using PRINT CHR$ with a keyword value greater than 164 simply displays the keyword and does not bring it into operation.

Format

  • CHR$ int-num-const [;] [+]
  • CHR$ int-num-var [;] [+]
  • CHR$ (int-num-expr) [;] [+]

See also

Chapter 14.