Q(uick)BASIC Function: CHR$

Quick View

CHR$ Function

A string processing function that returns a one-character string whose ASCII code is the argument

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • ASC(stringexpression$)
  • CHR$(ascii-code%)
Description/Parameter(s)
stringexpression$ Any string expression.
ascii-code% The ASCII code of the desired character.

ASC returns the ASCII code for the first character in a string expression.

CHR$ returns the character corresponding to a specified ASCII code.

Example
PRINT ASC("Q") 'Output is: 81 PRINT CHR$(65) 'Output is: A
Syntax
  • CHR$(code)
Description/Parameter(s)
code A numeric expression that has a value between 0 and 255, is one of the ASCII character codes

CHR$ is commonly used to send a special character to the screen or printer. For example, you can send a form feed (CHR$(12)) to clear the screen and return the cursor to the home position.

CHR$ can also be used to include a double quote (") in a string:

  • Msg$=CHR$(34)+"Quoted string"+CHR$(34)

This line adds a double-quote character to the beginning and the end of the string.

Example

CHR_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the CHR$ function. To look at the program in the View window and, optionally, to run it, load CHR_EX.BAS using the File menu's Open Program command.
The program uses CHR$ to display graphics characters in the extended character set, and uses these characters to draw boxes on the screen.

Syntax
  • CHR$(code%)
Description/Parameter(s)
code% An ASCII value between 0 and 255, inclusive.

Usage Notes

  • CHR$ is commonly used to send a special character to the screen or printer. For example, you can send a form feed (CHR$(12)) to clear the screen and return the cursor to the home position.
  • CHR$ also can be used to include a double quotation mark (") in a string:

  • Msg$ = CHR$(34) + "Quoted string" + CHR$(34)

  • This line adds a double-quotation-mark character to the beginning and end of the string.
Example

This example uses the CHR$ function to display graphics characters in the extended character set, using these characters to draw boxes on the screen.

DEFINT A-Z 'Display two double-sided boxes. CLS CALL DBox(5, 22, 18, 40) CALL DBox(1, 4, 4, 50) END SUB DBox (Urow%, Ucol%, Lrow%, Lcol%) STATIC 'Parameters: ' Urow%, Ucol% : Row and column of upper-left corner. ' Lrow%, Lcol% : Row and column of lower-right corner. CONST ULEFTC = 201, URIGHTC = 187, LLEFTC = 200, LRIGHTC = 188 'Constants for extended ASCII graphic characters. CONST VERTICAL = 186, HORIZONTAL = 205 'Draw top of box. LOCATE Urow%, Ucol%: PRINT CHR$(ULEFTC); LOCATE , Ucol% + 1: PRINT STRING$(Lcol% - Ucol%, CHR$(HORIZONTAL)); LOCATE , Lcol%: PRINT CHR$(URIGHTC); 'Draw body of box. FOR I = Urow% + 1 TO Lrow% - 1 LOCATE I, Ucol%: PRINT CHR$(VERTICAL); LOCATE , Lcol%: PRINT CHR$(VERTICAL); NEXT I 'Draw bottom of box. LOCATE Lrow%, Ucol%: PRINT CHR$(LLEFTC); LOCATE , Ucol% + 1: PRINT STRING$(Lcol% - Ucol%, CHR$(HORIZONTAL)); LOCATE , Lcol%: PRINT CHR$(LRIGHTC); END SUB