CHR$

From C64-Wiki
Jump to navigationJump to 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 character (of string type) and is the inverse function of ASC.

The argument expression is implicitly taken as integer value. Numbers outside the range - either negative or greater than 255 - leads to the BASIC error ?ILLEGAL QUANTITY ERROR. If an incorrect variable type is used, the BASIC error ?TYPE MISMATCH ERROR appears. In case the numerical argument is missing, the BASIC error ?SYNTAX ERROR is issued.

If you include the converted ASCII code of a control character, its function 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