CHR$

From C64-Wiki
Jump to: navigation, search
BASIC keyword
Keyword: CHR$
Abbreviation: C, Shift+H
Type: Function
Token code: 199/$C7
Handling routine
in BASIC ROM:
46828–46847
$B6EC–B6FF
List of all BASIC keywords


Remark: This article describes the BASIC function CHR$ in BASIC V2 at the Commodore 64.

Type: String Function 
General Programming-Syntax: CHR$(<numeric-expression>)

CHR$ can be used to convert a number between 0 and 255 into an ASCII-char and is the inverse of the function ASC.

Numbers outside the range - either negative or greater than 255 - produce the BASIC-error ?ILLEGAL QUANTITY ERROR IN line. When using a wrong type of variable the BASIC-error ?TYPE MISMATCH ERROR IN line appears. When the numerical argument is missing, the BASIC-error ?SYNTAX ERROR IN line is issued.

If you include the converted ASCII-Code of a control character, the control char can be executed by using the BASIC command PRINT.

Examples[edit]

Control characters:

10 PRINT CHR$(147): REM clear the screen
20 A$="First name"+CHR$(13)+"Surname": REM adding the <RETURN>-key into the string A$.
30 PRINT A$: REM output A$

Special unquoteable characters:

10 INPUT "Name:";N$
20 REM to get a double quote we need the PETSCII code for (")
30 PRINT "Your name is ";CHR$(34);N$;CHR$(34)

giving output (for input "Peter")

Your name is "Peter"
BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT