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%}}])


* {{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.
* 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. 
* 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$) &lt;nowiki>=&lt;/nowiki> 0 THEN byte2 &lt;nowiki>=&lt;/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''.


{{PageErrors}}
* 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.
* '''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.


{{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&amp;  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   ,&lt;  .>  /?    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)'''
'{{small|NOTE: The above commented table can be copied and pasted directly into the QB64 IDE}}
{{WhiteEnd}} 


&lt;center>'''[[ASCII#Two_Byte_Codes|Two Byte Ctrl, Alt and Shift + Function key combinations]]'''&lt;/center>

{{WhiteStart}}                    '''Two Byte Characters        Key                 CHR$(0) + "?" '''

                    CHR$(0) + CHR$(16-50)      [Alt] + letter       
                    CHR$(0) + CHR$(59)         [F1]                 ";"
                    CHR$(0) + CHR$(60)         [F2]                 "&lt;"
                    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]        "î"
{{WhiteEnd}}
:In '''QB64''', [[CVI]] can be used to get the [[_KEYDOWN]] 2-byte code value. Example: IF _KEYDOWN([[CVI]]([[CHR$]](0) + "P")) THEN


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

''Returns:''
{{OutputStart}}
 65
 66
 67
{{OutputEnd}}

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

{{CodeStart}} '' ''
Q$ = {{Cl|CHR$}}(34) ' quote character
{{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|DO}}
   {{Cl|DO}}: {{Cl|SLEEP}}: key$ = {{Cl|INKEY$}}: {{Cl|LOOP}} {{Cl|UNTIL}} key$ &lt;> "" ' prevent ASC empty string read error
   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|PRINT}} "{{Cl|CHR$}}(" + {{Cl|LTRIM$}}({{Cl|STR$}}(code%)) + ")" + {{Cl|SPACE$}}(13): 
    {{Cl|IF...THEN|IF}} code% > 8 {{Cl|AND (boolean)|AND}} code% &lt; 14 {{Cl|THEN}} code% = 32    ' unprintable control codes
    {{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|COLOR}} 14: {{Cl|LOCATE}} 10, 50: {{Cl|PRINT}} "{{Cl|CHR$}}(0) + " + Q$ + {{Cl|CHR$}}({{Cl|ASC}}(key$, 2)) + Q$
   {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} code% = 27 '' '

{{CodeEnd}}
{{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.


''Example 3:'' Reading only numerical values input by a program user.
{{CodeStart}}
 {{Cl|DO}}: {{Cl|SLEEP}} ' requires a keypress to run loop once
   K$ = {{Cl|INKEY$}}
   code = {{Cl|ASC}}(K$)
   {{Cl|IF}} code >= 48 {{Cl|AND (boolean)|AND}} code &lt;= 57 {{Cl|THEN}} entry$ = entry$ + {{Cl|CHR$}}(code) ' numbers only
   {{Cl|IF}} code = 46 {{Cl|AND (boolean)|AND}} flag = 0 {{Cl|THEN}} 
      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 &lt; 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.


{{Parameter|See also:'' }}
* [[ASC (statement)]]
* [[_KEYHIT]], [[_KEYDOWN]]
* [[MID$]], [[CHR$]], [[INKEY$]]
* [[VAL]], [[STRING$]]
* [[ASCII]], [[_MAPUNICODE]]
* [[Scancodes]]


{{PageNavigation}}
