ASC (function): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 19: Line 19:


{{PageExamples}}
{{PageExamples}}
;Example:How ASC can be used to find any ASCII code in a string of characters using QB64.
;Example 1:How ASC can be used to find any ASCII code in a string of characters using QB64.
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Cl|ASC (function)|ASC}}("A")
{{Cl|PRINT}} {{Cl|ASC (function)|ASC}}("A")
Line 38: Line 38:
----
----


Example 2:Reading the ASCII and two byte code combinations returned by [[INKEY$]] with '''ASC''' in '''QB64'''.
;Example 2:Reading the ASCII and two byte code combinations returned by [[INKEY$]] with '''ASC''' in '''QB64'''.
{{CodeStart}}
{{CodeStart}}
Q$ = {{Cl|CHR$}}(34) ' quote character
Q$ = {{Cl|CHR$}}(34) ' quote character
Line 69: Line 69:


{{PageSeeAlso}}
{{PageSeeAlso}}
* [https://qb64phoenix.com/forum/showthread.php?tid=1149 Featured in our "Keyword of the Day" series]
* [[ASC]]
* [[ASC]]
* [[_KEYHIT]], [[_KEYDOWN]]
* [[_KEYHIT]], [[_KEYDOWN]]

Latest revision as of 19:53, 24 May 2024

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


Syntax

code% = ASC(text$[, position%])


Description

  • The text$ parameter must have a length of at least 1 byte or an error occurs.
  • In QB64 only the optional position% parameter specifies the character in a string to be returned. Must be greater than 0.
  • If the optional position% parameter is omitted, ASC will return the ASCII code of the first character.
  • ASCII code values returned range from 0 to 255.
  • In QB64, the ASC function reads string byte positions about 5 times faster than MID$ when parsing strings character wise. See MID$ Example 2.

Errors

  • If the function is used to read an empty string value an Illegal function call error will occur.
  • The QB64 position% parameter must range from 1 to the length of the string being read or an Illegal function call error will occur.


Examples

Example 1
How ASC can be used to find any ASCII code in a string of characters using QB64.
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.

Example 2
Reading the ASCII and two byte code combinations returned by INKEY$ 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: 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. 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.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link