- 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 parameter is allowed; position, the ASCII code of the character in the character position specified will be returned by the function (if omitted ASC will return the code of the first character).
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 byte codes with ASC in QB64.
Q$ = CHR$(34) COLOR 11: LOCATE 5, 22: PRINT "Press some keys or combinations!" 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 10: LOCATE 10, 10 IF code% THEN ' ASC returns any value greater than 0 PRINT "CHR$(" + LTRIM$(STR$(code%)) + ")" + SPACE$(13): LOCATE 10, 50: PRINT 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 ASC(key$) = 27
Explanation: The keypress read loop checks that ASC will not read an empty string. That would create a program error. SLEEP reduces CPU memory useage 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. Also displays two byte string equivalent.
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!