ASC: Difference between revisions
m (Removed protection from "ASC") |
No edit summary |
||
Line 6: | Line 6: | ||
* {{Parameter|text$}} [[STRING|string]] character parameter must have a length of at least 1 byte or an error occurs. | * {{Parameter|text$}} [[STRING|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 of any character in a string to be returned. | * '''In QB64''' the optional byte {{Parameter|position%}} [[INTEGER]] parameter greater than 0 can specify the ASCII code of any character in a string to be returned. | ||
* If the optional {{Parameter|position%}} parameter is omitted, ASC will return the [[ASCII]] code of the first [[STRING]] character. | * If the optional {{Parameter|position%}} parameter is omitted, ASC will return the [[ASCII]] code of the first [[STRING]] character. | ||
* [[ASCII]] code [[INTEGER]] or [[_UNSIGNED]] [[_BYTE]] values returned range from 0 to 255. | * [[ASCII]] code [[INTEGER]] or [[_UNSIGNED]] [[_BYTE]] values returned range from 0 to 255. | ||
* ASC returns 0 when reading [[ASCII]] 2 byte codes returned by [[INKEY$]] when the arrow, function, Home/Page keys are used. | * ASC returns 0 when reading [[ASCII]] 2 byte codes returned by [[INKEY$]] when the arrow, function, Home/Page keys are used. | ||
** Use QB64's {{Parameter|position%}} parameter to read the second byte if necessary. {{Text|IF ASC(key$) <nowiki>=</nowiki> 0 THEN byte2 <nowiki>=</nowiki> ASC(key$, 2)|green}} | ** Use QB64's {{Parameter|position%}} parameter to read the second byte if necessary. {{Text|IF ASC(key$) <nowiki>=</nowiki> 0 THEN byte2 <nowiki>=</nowiki> ASC(key$, 2)|green}} | ||
* In '''QB64''' ASC string byte position reads are about '''5 times faster''' than [[MID$]] when parsing strings. See [[MID$]] ''Example 2''. | * In '''QB64''' ASC string byte position reads are about '''5 times faster''' than [[MID$]] when parsing strings. See [[MID$]] ''Example 2''. | ||
Line 22: | Line 22: | ||
{{WhiteStart}}' '''ASCII Keyboard Codes''' | {{WhiteStart}}' '''ASCII Keyboard Codes''' | ||
' | ' | ||
'''' Esc F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Sys ScL Pause''' | '''' Esc F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Sys ScL Pause''' | ||
' 27 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +133 +134 - - - | ' 27 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +133 +134 - - - | ||
'''' `~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) -_ =+ BkSp Ins Hme PUp NumL / * -''' | '''' `~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) -_ =+ BkSp Ins Hme PUp NumL / * -''' | ||
' 126 33 64 35 36 37 94 38 42 40 41 95 43 8 +82 +71 +73 - 47 42 45 | ' 126 33 64 35 36 37 94 38 42 40 41 95 43 8 +82 +71 +73 - 47 42 45 | ||
''' 96 49 50 51 52 53 54 55 56 57 48 45 61'' | ''' 96 49 50 51 52 53 54 55 56 57 48 45 61'' | ||
Line 30: | Line 30: | ||
' 9 81 87 69 82 84 89 85 73 79 80 123 125 124 +83 +79 +81 +71 +72 +73 43 | ' 9 81 87 69 82 84 89 85 73 79 80 123 125 124 +83 +79 +81 +71 +72 +73 43 | ||
''' 113 119 101 114 116 121 117 105 111 112 91 93 92 55 56 57 '' | ''' 113 119 101 114 116 121 117 105 111 112 91 93 92 55 56 57 '' | ||
'''' CapL A S D F G H J K L ;: '" Enter 4/◄- 5 6/-► | '''' CapL A S D F G H J K L ;: '" Enter 4/◄- 5 6/-► | ||
' - 65 83 68 70 71 72 74 75 76 58 34 13 +75 +76 +77 '''E''' | ' - 65 83 68 70 71 72 74 75 76 58 34 13 +75 +76 +77 '''E''' | ||
''' 97 115 100 102 103 104 106 107 108 59 39 52 53 54 '' '''n''' | ''' 97 115 100 102 103 104 106 107 108 59 39 52 53 54 '' '''n''' | ||
'''' Shift Z X C V B N M ,< .> /? Shift ▲ 1End 2/▼ 3PD t''' | '''' Shift Z X C V B N M ,< .> /? Shift ▲ 1End 2/▼ 3PD t''' | ||
' * 90 88 67 86 66 78 77 60 62 63 * +72 +79 +80 +81 '''e''' | ' * 90 88 67 86 66 78 77 60 62 63 * +72 +79 +80 +81 '''e''' | ||
''' 122 120 99 118 98 110 109 44 46 47 49 50 51 '' '''r''' | ''' 122 120 99 118 98 110 109 44 46 47 49 50 51 '' '''r''' | ||
'''' Ctrl Win Alt Spacebar Alt Win Menu Ctrl ◄- ▼ -► 0Ins .Del ''' | '''' Ctrl Win Alt Spacebar Alt Win Menu Ctrl ◄- ▼ -► 0Ins .Del ''' | ||
' * - * 32 * - - * +75 +80 +77 +82 +83 13 | ' * - * 32 * - - * +75 +80 +77 +82 +83 13 | ||
' '' 48 46'' | ' '' 48 46'' | ||
' | ' | ||
' ''' ''Italics'' = LCase/NumLock On ____________ + = 2 Byte: CHR$(0) + CHR$(code)''' | ' ''' ''Italics'' = LCase/NumLock On ____________ + = 2 Byte: CHR$(0) + CHR$(code)''' | ||
'{{small|NOTE: The above commented table can be copied and pasted directly into the QB64 IDE}} | '{{small|NOTE: The above commented table can be copied and pasted directly into the QB64 IDE}} | ||
{{WhiteEnd}} | {{WhiteEnd}} | ||
Line 49: | Line 49: | ||
{{WhiteStart}} '''Two Byte Characters Key CHR$(0) + "?" ''' | {{WhiteStart}} '''Two Byte Characters Key CHR$(0) + "?" ''' | ||
CHR$(0) + CHR$(16-50) [Alt] + letter | CHR$(0) + CHR$(16-50) [Alt] + letter | ||
CHR$(0) + CHR$(59) [F1] ";" | CHR$(0) + CHR$(59) [F1] ";" | ||
CHR$(0) + CHR$(60) [F2] "<" | CHR$(0) + CHR$(60) [F2] "<" | ||
Line 74: | Line 74: | ||
CHR$(0) + CHR$(94-103) [Ctrl] + F1-10 | CHR$(0) + CHR$(94-103) [Ctrl] + F1-10 | ||
CHR$(0) + CHR$(104-113) [Alt] + F1-10 | CHR$(0) + CHR$(104-113) [Alt] + F1-10 | ||
CHR$(0) + CHR$(114-119) [Ctrl] + keypad | CHR$(0) + CHR$(114-119) [Ctrl] + keypad | ||
CHR$(0) + CHR$(120-129) [Alt] + number | CHR$(0) + CHR$(120-129) [Alt] + number | ||
CHR$(0) + CHR$(130 or 131) [Alt] + _/- or +/= "é" or "â" | CHR$(0) + CHR$(130 or 131) [Alt] + _/- or +/= "é" or "â" | ||
Line 91: | Line 91: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' 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}} ASC("A") | {{Cl|PRINT}} {{Cl|ASC}}("A") | ||
{{Cl|PRINT}} ASC("Be a rockstar") | {{Cl|PRINT}} {{Cl|ASC}}("Be a rockstar") | ||
{{Cl|PRINT}} ASC("QB64 is not only COMPATIBLE, it can find any part of the string!", 18) | {{Cl|PRINT}} {{Cl|ASC}}("QB64 is not only COMPATIBLE, it can find any part of the string!", 18) | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 111: | Line 111: | ||
''Example 2:'' Reading the ASCII and two byte code combinations with ASC in '''QB64'''. | ''Example 2:'' Reading the ASCII and two byte code combinations with ASC in '''QB64'''. | ||
{{CodeStart}} | {{CodeStart}} | ||
Q$ = {{Cl|CHR$}}(34) ' quote character | Q$ = {{Cl|CHR$}}(34) ' quote character | ||
{{Cl|COLOR}} 10: {{Cl|LOCATE}} 5, 22: {{Cl|PRINT}} "Press some keys or combinations!" | {{Cl|COLOR}} 10: {{Cl|LOCATE}} 5, 22: {{Cl|PRINT}} "Press some keys or combinations!" | ||
{{Cl|COLOR}} 13: {{Cl|LOCATE}} 23, 30: {{Cl|PRINT}} "Escape key Quits" | {{Cl|COLOR}} 13: {{Cl|LOCATE}} 23, 30: {{Cl|PRINT}} "Escape key Quits" | ||
DO | {{Cl|DO}} | ||
DO: {{Cl|SLEEP}}: key$ = {{Cl|INKEY$}}: {{Cl|LOOP}} {{Cl|UNTIL}} key$ <> "" ' prevent ASC empty string read error | {{Cl|DO}}: {{Cl|SLEEP}}: 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}}(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}}(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}}(key$, 2)) + Q$ | ||
Line 134: | Line 134: | ||
''Example 3:'' Reading only numerical values input by a program user. | ''Example 3:'' Reading only numerical values input by a program user. | ||
{{CodeStart}} | {{CodeStart}} | ||
DO: {{Cl|SLEEP}} ' requires a keypress to run loop once | {{Cl|DO}}: {{Cl|SLEEP}} ' requires a keypress to run loop once | ||
K$ = | K$ = {{Cl|INKEY$}} | ||
code = {{Cl|ASC}}(K$) | code = {{Cl|ASC}}(K$) | ||
{{Cl | {{Cl|IF}} code >= 48 {{Cl|AND (boolean)|AND}} code <= 57 {{Cl|THEN}} entry$ = entry$ + {{Cl|CHR$}}(code) ' numbers only | ||
{{Cl | {{Cl|IF}} code = 46 {{Cl|AND (boolean)|AND}} flag = 0 {{Cl|THEN}} | ||
entry$ = entry$ + K$: flag = 1: mark = {{Cl|LEN}}(entry$) ' decimal point | entry$ = entry$ + K$: flag = 1: mark = {{Cl|LEN}}(entry$) ' decimal point | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
L = | L = {{Cl|LEN}}(entry$) ' check entry length for possible backspace | ||
{{Cl | {{Cl|IF}} code = 8 {{Cl|AND (boolean)|AND}} L > 0 {{Cl|THEN}} ' backspace pressed and entry has a length | ||
entry$ = | entry$ = {{Cl|MID$}}(entry$, 1, L - 1) ' remove one character from entry$ | ||
{{Cl | {{Cl|IF}} L - 1 < mark {{Cl|THEN}} flag = 0 ' allow another decimal point if removed. | ||
{{Cl|LOCATE}} 10, {{Cl|POS}}(0) - 1: {{Cl|PRINT}} | {{Cl|LOCATE}} 10, {{Cl|POS}}(0) - 1: {{Cl|PRINT}} {{Cl|SPACE$}}(1); ' remove character from screen | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|LOCATE}} 10, 10: {{Cl|PRINT}} entry$; | {{Cl|LOCATE}} 10, 10: {{Cl|PRINT}} entry$; | ||
' display present entry to user(semicolon required for correct POS return) | ' display present entry to user(semicolon required for correct POS return) | ||
{{Cl|LOOP}} {{Cl|UNTIL}} code = 13 {{Cl|AND}} L | {{Cl|LOOP}} {{Cl|UNTIL}} code = 13 {{Cl|AND}} L | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 165: | Line 165: | ||
{{PageNavigation}} | {{PageNavigation}} | ||
[[Category:Latest]] |
Revision as of 22:27, 9 July 2022
The ASC function returns the ASCII code number of a certain STRING text character or a keyboard press.
Syntax
- code% = ASC(text$[, position%])
- text$ string character parameter must have a length of at least 1 byte or an error occurs.
- In QB64 the optional byte position% INTEGER parameter greater than 0 can specify the ASCII code of any character in a string to be returned.
- If the optional position% parameter is omitted, ASC will return the ASCII code of the first STRING character.
- ASCII code INTEGER or _UNSIGNED _BYTE values returned range from 0 to 255.
- ASC returns 0 when reading ASCII 2 byte codes returned by INKEY$ when the arrow, function, Home/Page keys are used.
- Use QB64's position% parameter to read the second byte if necessary. IF ASC(key$) = 0 THEN byte2 = ASC(key$, 2)
- In QB64 ASC string byte position reads are about 5 times faster than MID$ when parsing strings. See MID$ Example 2.
- If the function is used to read an empty string value an illegal function call error will occur. INKEY$ returns an empty string when a key is not pressed.
- QB64's position% parameters must range from 1 to the length of the string being read or an illegal function call error will occur.
Template:WhiteStart' ASCII Keyboard Codes
'
' Esc F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Sys ScL Pause
' 27 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +133 +134 - - -
' `~ 1! 2@ 3# 4$ 5% 6^ 7& 8* 9( 0) -_ =+ BkSp Ins Hme PUp NumL / * -
' 126 33 64 35 36 37 94 38 42 40 41 95 43 8 +82 +71 +73 - 47 42 45
' 96 49 50 51 52 53 54 55 56 57 48 45 61
' Tab Q W E R T Y U I O P [{ ]} \| Del End PDn 7Hme 8/▲ 9PU +
' 9 81 87 69 82 84 89 85 73 79 80 123 125 124 +83 +79 +81 +71 +72 +73 43
' 113 119 101 114 116 121 117 105 111 112 91 93 92 55 56 57
' CapL A S D F G H J K L ;: '" Enter 4/◄- 5 6/-►
' - 65 83 68 70 71 72 74 75 76 58 34 13 +75 +76 +77 E
' 97 115 100 102 103 104 106 107 108 59 39 52 53 54 n
' Shift Z X C V B N M ,< .> /? Shift ▲ 1End 2/▼ 3PD t
' * 90 88 67 86 66 78 77 60 62 63 * +72 +79 +80 +81 e
' 122 120 99 118 98 110 109 44 46 47 49 50 51 r
' Ctrl Win Alt Spacebar Alt Win Menu Ctrl ◄- ▼ -► 0Ins .Del
' * - * 32 * - - * +75 +80 +77 +82 +83 13
' 48 46
'
' Italics = LCase/NumLock On ____________ + = 2 Byte: CHR$(0) + CHR$(code)
'
Template:WhiteStart Two Byte Characters Key CHR$(0) + "?"
CHR$(0) + CHR$(16-50) [Alt] + letter CHR$(0) + CHR$(59) [F1] ";" CHR$(0) + CHR$(60) [F2] "<" CHR$(0) + CHR$(61) [F3] "=" CHR$(0) + CHR$(62) [F4] ">" CHR$(0) + CHR$(63) [F5] "?" CHR$(0) + CHR$(64) [F6] "@" CHR$(0) + CHR$(65) [F7] "A" CHR$(0) + CHR$(66) [F8] "B" CHR$(0) + CHR$(67) [F9] "C" CHR$(0) + CHR$(68) [F10] "D" CHR$(0) + CHR$(71) [Home] "G" CHR$(0) + CHR$(72) [↑] Arrow "H" CHR$(0) + CHR$(73) [Page Up] "I" CHR$(0) + CHR$(75) [←] Arrow "K" CHR$(0) + CHR$(76) [5 NumberPad] "L" (NumLock off in QB64) CHR$(0) + CHR$(77) [→] Arrow "M" CHR$(0) + CHR$(79) [End] "O" CHR$(0) + CHR$(80) [↓] Arrow "P" CHR$(0) + CHR$(81) [Page Down] "Q" CHR$(0) + CHR$(82) [Insert] "R" CHR$(0) + CHR$(83) [Delete] "S" CHR$(0) + CHR$(84-93) [Shift] + F1-10 CHR$(0) + CHR$(94-103) [Ctrl] + F1-10 CHR$(0) + CHR$(104-113) [Alt] + F1-10 CHR$(0) + CHR$(114-119) [Ctrl] + keypad CHR$(0) + CHR$(120-129) [Alt] + number CHR$(0) + CHR$(130 or 131) [Alt] + _/- or +/= "é" or "â" CHR$(0) + CHR$(133) [F11] "à" CHR$(0) + CHR$(134) [F12] "å" CHR$(0) + CHR$(135) [Shift] + [F11] "ç" CHR$(0) + CHR$(136) [Shift] + [F12] "ê" CHR$(0) + CHR$(137) [Ctrl] + [F11] "ë" CHR$(0) + CHR$(138) [Ctrl] + [F12] "è" CHR$(0) + CHR$(139) [Alt] + [F11] "ï" CHR$(0) + CHR$(140) [Alt] + [F12] "î"
- In QB64, CVI can be used to get the _KEYDOWN 2-byte code value. Example: IF _KEYDOWN(CVI(CHR$(0) + "P")) THEN
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) |
Returns:
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 ' |
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.
See also: