ASC (function): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(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
Line 1: Line 1:
The [[ASC]] function returns the [[ASCII]] code number of a certain [[STRING]] text character or a keyboard press.
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$}} [[STRING|string]] character parameter must have a length of at least 1 byte or an error occurs.
* The {{Parameter|text$}} 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 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 [[STRING]] character.
* If the optional {{Parameter|position%}} parameter is omitted, '''ASC''' will return the [[ASCII]] code of the first character.
* [[ASCII]] code [[INTEGER]] or [[_UNSIGNED]] [[_BYTE]] values returned range from 0 to 255.
* [[ASCII]] code 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.
* 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''.
** 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''.


=== Errors ===
=== Errors ===
* If the function is used to read an '''empty string value''' an illegal function call [[ERROR Codes|error]] will occur. [[INKEY$]] returns an empty string when a key is not pressed.
* If the function is used to read an '''empty string value''' an [[ERROR Codes|Illegal function call]] error will occur.
* '''QB64''''s {{Parameter|position%}} parameters must range from 1 to the [[LEN|length]] of the string being read or an illegal function call [[ERROR Codes|error]] will occur.
* 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.
 
 
{{FixedStart}}'                                '''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)'''
{{FixedEnd}}
{{Small|NOTE: The above commented table can be copied and pasted directly into the QB64 IDE}}
 
 
<center>'''[[ASCII#Two_Byte_Codes|Two Byte Ctrl, Alt and Shift + Function key combinations]]'''</center>
 
{{FixedStart}}                    '''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]        "î"
{{FixedEnd}}
:In '''QB64''', [[CVI]] can be used to get the [[_KEYDOWN]] 2-byte code value. Example: IF _KEYDOWN([[CVI]]([[CHR$]](0) + "P")) THEN




{{PageExamples}}
{{PageExamples}}
''Example 1:'' How ASC can be used to find any ASCII code in a string of characters using QB64.
;Example:How ASC can be used to find any ASCII code in a string of characters using QB64.
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Cl|ASC}}("A")
{{Cl|PRINT}} {{Cl|ASC (function)|ASC}}("A")
{{Cl|PRINT}} {{Cl|ASC}}("Be a rockstar")
{{Cl|PRINT}} {{Cl|ASC (function)|ASC}}("Be a rockstar")
{{Cl|PRINT}} {{Cl|ASC}}("QB64 is not only COMPATIBLE, it can find any part of the string!", 18)
{{Cl|PRINT}} {{Cl|ASC (function)|ASC}}("QB64 is not only COMPATIBLE, it can find any part of the string!", 18)
{{CodeEnd}}
{{CodeEnd}}
''Returns:''
{{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}}


''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'''.


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|SLEEP}}: key$ = {{Cl|INKEY$}}: {{Cl|LOOP}} {{Cl|UNTIL}} key$ <> "" ' prevent ASC empty string read error
   {{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:'' 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.
{{PreStart}}
 
'''Explanation'''
 
The keypress read loop checks that '''ASC''' will not read an empty
''Example 3:'' Reading only numerical values input by a program user.
string. That would create a program error. Normal byte codes returned
{{CodeStart}}
are indicated by the '''IF''' statement when '''ASC''' returns a value.
{{Cl|DO}}: {{Cl|SLEEP}} ' requires a keypress to run loop once
Otherwise the routine will return the two byte ASCII code. The
  K$ = {{Cl|INKEY$}}
extended keyboard keys (Home pad, Arrow pad and Number pad), Function
  code = {{Cl|ASC}}(K$)
keys or Ctrl, Alt or Shift key combinations will return two byte codes.
  {{Cl|IF}} code >= 48 {{Cl|AND (boolean)|AND}} code <= 57 {{Cl|THEN}} entry$ = entry$ + {{Cl|CHR$}}(code) ' numbers only
Ctrl + letter combinations will return control character codes 1 to 26.
  {{Cl|IF}} code = 46 {{Cl|AND (boolean)|AND}} flag = 0 {{Cl|THEN}}
{{PreEnd}}
      entry$ = entry$ + K$: flag = 1: mark = {{Cl|LEN}}(entry$) ' decimal point
  {{Cl|END IF}}
  L = {{Cl|LEN}}(entry$) ' check entry length for possible backspace
  {{Cl|IF}} code = 8 {{Cl|AND (boolean)|AND}} L > 0 {{Cl|THEN}} ' backspace pressed and entry has a length
    entry$ = {{Cl|MID$}}(entry$, 1, L - 1) ' remove one character from entry$
    {{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|SPACE$}}(1); ' remove character from screen
  {{Cl|END IF}}
  {{Cl|LOCATE}} 10, 10: {{Cl|PRINT}} entry$;
  ' display present entry to user(semicolon required for correct POS return)
{{Cl|LOOP}} {{Cl|UNTIL}} code = 13 {{Cl|AND}} L
{{CodeEnd}}
 
''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.




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[ASC (statement)]]
* [[ASC]]
* [[_KEYHIT]], [[_KEYDOWN]]
* [[_KEYHIT]], [[_KEYDOWN]]
* [[MID$]], [[CHR$]], [[INKEY$]]
* [[MID$]], [[MID$ (function)]]
* [[CHR$]], [[INKEY$]]
* [[VAL]], [[STRING$]]
* [[VAL]], [[STRING$]]
* [[ASCII]], [[_MAPUNICODE]]
* [[ASCII]], [[_MAPUNICODE]]

Revision as of 00:29, 26 February 2023

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
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