ASC (function): Difference between revisions
Jump to navigation
Jump to search
Code by Ted Weissgerber
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "The ASC function returns the ASCII code number of a certain STRING text character or a keyboard press. {{PageSyntax}} : {{Parameter|code%}} = ASC({{Parameter|text$}}[, {{Parameter|position%}}]) {{PageDescription}} * {{Parameter|text$}} string character parameter must have a length of at least 1 byte or an error occurs. * '''In QB64''' the optional byte {{Parameter|position%}} INTEGER parameter greater than 0 can specify the ASCII code o...") |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
The | The '''ASC''' function returns the [[ASCII]] code number of a certain [[STRING]] text character. | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: {{Parameter|code%}} = [[ASC]]({{Parameter|text$}}[, {{Parameter|position%}}]) | : {{Parameter|code%}} = [[ASC (function)|ASC]]({{Parameter|text$}}[, {{Parameter|position%}}]) | ||
{{PageDescription}} | {{PageDescription}} | ||
* {{Parameter|text$}} | * The {{Parameter|text$}} parameter must have a length of at least 1 byte or an error occurs. | ||
* '''In QB64''' the optional | * '''In QB64 only''' the optional {{Parameter|position%}} parameter specifies the character in a string to be returned. Must be greater than 0. | ||
* If the optional {{Parameter|position%}} parameter is omitted, ASC will return the [[ASCII]] code of the first | * If the optional {{Parameter|position%}} parameter is omitted, '''ASC''' will return the [[ASCII]] code of the first character. | ||
* [[ASCII]] code | * [[ASCII]] code values returned range from 0 to 255. | ||
* | * In '''QB64''', the '''ASC''' function reads string byte positions about 5 times faster than [[MID$ (function)|MID$]] when parsing strings character wise. See [[MID$ (function)|MID$]] ''Example 2''. | ||
=== Errors === | === Errors === | ||
* If the function is used to read an '''empty string value''' an | * If the function is used to read an '''empty string value''' an [[ERROR Codes|Illegal function call]] error will occur. | ||
* '''QB64''' | * The '''QB64''' {{Parameter|position%}} parameter must range from 1 to the length of the string being read or an [[ERROR Codes|Illegal function call]] error will occur. | ||
{{PageExamples}} | {{PageExamples}} | ||
;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}}("Be a rockstar") | |||
{{Cl|PRINT}} {{Cl|ASC (function)|ASC}}("QB64 is not only COMPATIBLE, it can find any part of the string!", 18) | |||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
65 | 65 | ||
Line 103: | Line 30: | ||
67 | 67 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
{{PreStart}} | |||
'''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. | |||
{{PreEnd}} | |||
---- | |||
;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 116: | Line 44: | ||
{{Cl|COLOR}} 13: {{Cl|LOCATE}} 23, 30: {{Cl|PRINT}} "Escape key Quits" | {{Cl|COLOR}} 13: {{Cl|LOCATE}} 23, 30: {{Cl|PRINT}} "Escape key Quits" | ||
{{Cl|DO}} | {{Cl|DO}} | ||
{{Cl|DO | {{Cl|DO}}: key$ = {{Cl|INKEY$}}: {{Cl|LOOP}} {{Cl|UNTIL}} key$ <> "" ' prevent ASC empty string read error | ||
code% = {{Cl|ASC}}(key$): {{Cl|COLOR}} 11: {{Cl|LOCATE}} 10, 10 | code% = {{Cl|ASC (function)|ASC}}(key$): {{Cl|COLOR}} 11: {{Cl|LOCATE}} 10, 10 | ||
{{Cl|IF...THEN|IF}} code% {{Cl|THEN}} ' ASC returns any value greater than 0 | {{Cl|IF...THEN|IF}} code% {{Cl|THEN}} ' ASC returns any value greater than 0 | ||
{{Cl|PRINT}} "{{Cl|CHR$}}(" + {{Cl|LTRIM$}}({{Cl|STR$}}(code%)) + ")" + {{Cl|SPACE$}}(13): | {{Cl|PRINT}} "{{Cl|CHR$}}(" + {{Cl|LTRIM$}}({{Cl|STR$}}(code%)) + ")" + {{Cl|SPACE$}}(13): | ||
{{Cl|IF...THEN|IF}} code% > 8 {{Cl|AND (boolean)|AND}} code% < 14 {{Cl|THEN}} code% = 32 ' unprintable control codes | {{Cl|IF...THEN|IF}} code% > 8 {{Cl|AND (boolean)|AND}} code% < 14 {{Cl|THEN}} code% = 32 ' unprintable control codes | ||
{{Cl|COLOR}} 14: {{Cl|LOCATE}} 10, 50: {{Cl|PRINT}} {{Cl|CHR$}}(code%) + {{Cl|SPACE$}}(13) | {{Cl|COLOR}} 14: {{Cl|LOCATE}} 10, 50: {{Cl|PRINT}} {{Cl|CHR$}}(code%) + {{Cl|SPACE$}}(13) | ||
{{Cl|ELSE}}: {{Cl|PRINT}} "{{Cl|CHR$}}(0) + {{Cl|CHR$}}(" + {{Cl|LTRIM$}}({{Cl|STR$}}({{Cl|ASC}}(key$, 2))) + ")" | {{Cl|ELSE}}: {{Cl|PRINT}} "{{Cl|CHR$}}(0) + {{Cl|CHR$}}(" + {{Cl|LTRIM$}}({{Cl|STR$}}({{Cl|ASC (function)|ASC}}(key$, 2))) + ")" | ||
{{Cl|COLOR}} 14: {{Cl|LOCATE}} 10, 50: {{Cl|PRINT}} "{{Cl|CHR$}}(0) + " + Q$ + {{Cl|CHR$}}({{Cl|ASC}}(key$, 2)) + Q$ | {{Cl|COLOR}} 14: {{Cl|LOCATE}} 10, 50: {{Cl|PRINT}} "{{Cl|CHR$}}(0) + " + Q$ + {{Cl|CHR$}}({{Cl|ASC (function)|ASC}}(key$, 2)) + Q$ | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|LOOP}} {{Cl|UNTIL}} code% = 27 '' ' | {{Cl|LOOP}} {{Cl|UNTIL}} code% = 27 '' ' | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{Small|Code by Ted Weissgerber}} | {{Small|Code by Ted Weissgerber}} | ||
''Explanation | {{PreStart}} | ||
'''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. | |||
{{PreEnd}} | |||
{{ | |||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[ASC | * [https://qb64phoenix.com/forum/showthread.php?tid=1149 Featured in our "Keyword of the Day" series] | ||
* [[ASC]] | |||
* [[_KEYHIT]], [[_KEYDOWN]] | * [[_KEYHIT]], [[_KEYDOWN]] | ||
* [[MID$]], [[CHR$]], [[INKEY$]] | * [[MID$]], [[MID$ (function)]] | ||
* [[CHR$]], [[INKEY$]] | |||
* [[VAL]], [[STRING$]] | * [[VAL]], [[STRING$]] | ||
* [[ASCII]], [[_MAPUNICODE]] | * [[ASCII]], [[_MAPUNICODE]] |
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 ' |
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
- Featured in our "Keyword of the Day" series
- ASC
- _KEYHIT, _KEYDOWN
- MID$, MID$ (function)
- CHR$, INKEY$
- VAL, STRING$
- ASCII, _MAPUNICODE
- Scancodes