From QB64 Wiki
Revision as of 05:48, 14 October 2010 by imported>Clippy
Jump to navigation Jump to search

The ASC function returns the ASCII code number of a certain STRING text character or a keyboard press.

Syntax: code% = ASC(character$)
QB64 Syntax: code% = ASC(text$[, position])
  • String character parameter must have a length of at least 1 byte or an error occurs. Cannot read empty strings.
  • If the string used with ASC has more than one character, the first byte character code is returned.
  • Code INTEGER values returned will range from 0 to 255 only.
  • ASC will create an error if reading an empty string ("") returned in an INKEY$ loop when there is no keypress!
  • ASC returns 0 when reading 2 byte codes returned by INKEY$ when the arrow, funtion, central keys or certain combination keyboard presses are used.
  • In QB64 a new position parameter is allowed. The ASCII code of the character in a string position specified will be returned by the function (if omitted ASC will return the code of the first character).

Example 1:

PRINT ASC("A") PRINT ASC("Be a rockstar") PRINT ASC("QB64 is not only COMPATIBLE, it can find any part of the string!", 18)


65 66 67

Explanation: The ASCII code for "A" is 65 and the ASCII code for "B" is 66, ASCII code for "C" is 67 and the "C" is at position 18 in the string.

Note: The ASCII code for "A" and "a" are different by the value of 32, "A" + 32 is "a", 65("A") + 32 = 97("a").

Example 2: Reading the ASCII and two byte code combinations with ASC in QB64.

Q$ = CHR$(34) ' quote character COLOR 10: LOCATE 5, 22: PRINT "Press some keys or combinations!" COLOR 13: LOCATE 23, 30: PRINT "Escape key Quits" DO DO: SLEEP: key$ = INKEY$: LOOP UNTIL key$ <> "" ' prevent ASC empty string read error code% = ASC(key$): COLOR 11: LOCATE 10, 10 IF code% THEN ' ASC returns any value greater than 0 PRINT "CHR$(" + LTRIM$(STR$(code%)) + ")" + SPACE$(13): IF code% > 8 AND code% < 14 THEN code% = 32 ' unprintable control codes COLOR 14: LOCATE 10, 50: PRINT CHR$(code%) + SPACE$(13) ELSE: PRINT "CHR$(0) + CHR$(" + LTRIM$(STR$(ASC(key$, 2))) + ")" COLOR 14: LOCATE 10, 50: PRINT "CHR$(0) + " + Q$ + CHR$(ASC(key$, 2)) + Q$ END IF LOOP UNTIL code% = 27

Code by Ted Weissgerber

Explanation: The keypress read loop checks that ASC will not read an empty string. That would create a program error. SLEEP reduces CPU memory usage between keypresses. Normal byte codes returned are indicated by the IF statement when ASC returns a value. Otherwise the routine will return the two byte ASCII code. The extended keyboard keys(Home pad, Arrow pad and Number pad), Function keys or Ctrl, Alt or Shift key combinations will return two byte codes. Ctrl + letter combinations will return control character codes 1 to 26.

Example 3: Reading only numerical values input by a program user.

DO: SLEEP ' requires a keypress to run loop once K$ = INKEY$ code = ASC(K$) IF code >= 48 AND code <= 57 THEN entry$ = entry$ + CHR$(code) ' numbers only IF code = 46 AND flag = 0 THEN entry$ = entry$ + K$: flag = 1: mark = LEN(entry$) ' decimal point END IF L = LEN(entry$) ' check entry length for possible backspace IF code = 8 AND L > 0 THEN ' backspace pressed and entry has a length entry$ = MID$(entry$, 1, L - 1) ' remove one character from entry$ IF L - 1 < mark THEN flag = 0 ' allow another decimal point if removed. LOCATE 10, POS(0) - 1: PRINT SPACE$(1); ' remove character from screen END IF LOCATE 10, 10: PRINT entry$; ' display present entry to user(semicolon required for correct POS return) LOOP UNTIL code = 13 AND L

Explanation: SLEEP waits for a keypress allowing background programs to use the processor time. It also keeps the press in the keyboard buffer for INKEY$ to read and guarantees that ASC will not read an empty string value to create an error. Filtered codes 48 to 57 are only number characters. One decimal point is allowed by using the flag. Code 8 is a backspace request which is ignored if the entry has no characters. If it is allowed it removes the last character from the entry and the screen. The loop exits when the user presses the [Enter] key and the entry has a length. Try to do that with INPUT!

See also:

CHR$ (function), VAL, STRING$, ASCII, ASC (statement)(QB64)

Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page