ASCII

From QB64 Wiki

Jump to: navigation, search

ASCII and Extended Codes

Contents


MS-DOS code page 437:
ASCII Control 0 to 31   |        ASCII Keyboard Character Codes 32 to 127               
Code Character [key] Code Character Code Character Code Character
0        (NUL) 32  [Spacebar] 64  @ 96  `
1  ☺   (SOH) 33  ! 65  A 97  a
2  ☻   (STX) 34  " 66  B 98  b
3  ♥   (ETX) 35  # 67  C 99  c
4  ♦   (EOT) 36  $ 68  D 100  d
5  ♣   (ENQ) 37  % 69  E 101  e
6  ♠   (ACK) 38  & 70  F 102  f
7  •   (BEL)   BEEP 39  ' 71  G 103  g
8  ◘    [BackSpace] ** 40  ( 72  H 104  h
9  ○   (HT)    [TAB] ** 41  ) 73  I 105   i
10  ◙   (LineFeed)  ** 42  * 74  J 106  j
11  ♂   (VT) ** 43  + 75  K 107  k
12  ♀   (FormFeed) ** 44  , 76  L 108  l
13  ♪   (CR) [Enter] ** 45  - 77  M 109  m
14  ♫   (SO) 46  . 78  N 110  n
15  ☼   (SI) 47  / 79  O 111  o
16  ►   (DLE) 48  0 80  P 112  p
17  ◄   (DC1) 49  1 81  Q 113  q
18  ↕   (DC2) 50  2 82  R 114  r
19  ‼   (DC3) 51  3 83  S 115  s
20  ¶   (DC4) 52  4 84  T 116  t
21  §   (NAK) 53  5 85  U 117  u
22  ▬   (SYN) 54  6 86  V 118  v
23  ↨   (ETB) 55  7 87  W 119  w
24  ↑   (CAN) 56  8 88  X 120  x
25  ↓   (EM) 57  9 89  Y 121  y
26  →   (SUB)    EOF 58  : 90  Z 122  z
27  ←   (ESC)   [Esc] 59  ; 91  [ 123  {
28  ∟   (FS) ** 60  < 92  \ 124  |
29  ↔   (GS) ** 61  = 93  ] 125  }
30  ▲   (RS) ** 62  > 94  ^ 126  ~
31  ▼   (US) ** 63  ? 95  _ 127  ⌂ (DEL) *
IBM, International, graphical, mathematical and other characters
Code Character Code Character Code Character Code Character
128  Ç 160  á 192  └ 224  α
129  ü 161  í 193  ┴ 225  ß
130  é 162  ó 194  ┬ 226  Γ
131  â 163  ú 195  ├ 227  π
132  ä 164  ñ 196  ─ 228  Σ
133  à 165  Ñ 197  ┼ 229  σ
134  å 166  ª 198  ╞ 230  µ
135  ç 167  º 199  ╟ 231  τ
136  ê 168  ¿ 200  ╚ 232  Φ
137  ë 169  ⌐ 201  ╔ 233  Θ
138  è 170  ¬ 202  ╩ 234  Ω
139  ï 171  ½ 203  ╦ 235  δ
140  î 172  ¼ 204  ╠ 236  ∞
141  ì 173  ¡ 205  ═ 237  φ
142  Ä 174  «  206  ╬ 238  ε
143  Å 175  » 207  ╧ 239  ∩
144  É 176  ░ 208  ╨ 240  ≡
145  æ 177  ▒ 209  ╤ 241  ±
146  Æ 178  ▓ 210  ╥ 242  ≥
147  ô 179  │ 211  ╙</tt> 243  ≤
148  ö 180  ┤ 212  ╘ 244  ⌠
149  ò 181  ╡ 213  ╒ 245  ⌡
150  û 182  ╢ 214  ╓ 246  ÷
151  ù 183  ╖ 215  ╫ 247  ≈
152  ÿ 184  ╕ 216  ╪ 248  °
153  Ö 185  ╣ 217  ┘ 249  ∙
154  Ü 186  ║ 218  ┌ 250  ·
155  ¢ 187  ╗ 219  █ 251  √
156  £ 188  ╝ 220  ▄ 252  ⁿ
157  ¥ 189  ╜ 221  ▌ 253  ²
158  ₧ 190  ╛ 222  ▐ 254  ■
159  ƒ 191  ┐ 223  ▀ 255  NBSP ***
* DEL was used to erase paper tape data by punching out all of the 7 holes.
** Control characters 8 to 13 and 28 to 31 can move text formatting when PRINTed
*** NBSP is a Non-breaking Space used to indent text. Some browsers may handle this character differently
More information about ASCII Control Characters


For Unicode Characters see Unicode


ASCII in Text and Printing
  • Characters can be entered in by holding down Alt key and entering the code number on the Number pad.
  • Characters 0 and 255 can also be used to print spaces(32). Useful for making filenames harder to delete too.
  • Character 7 will create a BEEP sound when printed in full screen or an error sound in Windows using a SCREEN 0 window.
  • Control characters 8, 9, 10, 11, 12, 13, 29, 30 and 31 can affect the screen or file PRINT placements.
  • Character 8 can backspace one space when printed.
  • Character 9 can TAB space text when printed.
  • Character 10 moves the cursor or "line feeds" the printer head down one row
  • Character 12 acts like CLS when printed. "Form feeds" the page out of printers.
  • Character 13 is the cursor or "carriage return" to the start of a new line.
  • Characters 13 and 10 can be combined to create the CrLf carriage return used in files or printing. crlf$ = CHR$(13) + CHR$(10).
  • Character 26 can be used to designate the end of a file. See EOF.
  • All other characters are one byte and take up one space(byte) in a STRING value or variable.
  • Characters 176 to 223 can be used to create screen borders or boundaries in an ASCII game. See: SCREEN
  • Character 253(small 2) can be found as the first character byte of a BSAVEd image file opened in BINARY mode.


ASCII Character Usage
  • PRINTs characters to the screen or a file as well as creating borders, accented letters or character symbols.
  • BINARY files often store values below 256 in the one byte character. To read the value get the code with ASC.
  • Can be used to crudely encrypt a file so others cannot read it. See CHR$ example 2.

(Return to Table of Contents)

Two Byte Codes

Two Byte ASCII Keyboard Return Values
  • INKEY$ returns both bytes when two byte keys or key combinations are pressed. The second byte = RIGHT$(CHR$(code), 1)
  • If the character returned is a two byte code, ASC will return 0. Warning: ASC cannot read empty INKEY$ string values!
  • String values returned can be compared in an IF or SELECT CASE routine by using the actual string definitions such as:
IF INKEY$ = CHR$(0) + CHR$(80) THEN row = row + 1 or IF INKEY$ = CHR$(0) + "P" THEN row = row + 1


List of Two Byte Character Codes       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$(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$(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] "î"

See Scancodes for other keyboard function keys.

(Return to Table of Contents)

Example

Example: Routine displays all keypress codes including Ctrl, Alt and Shift combinations. Ctrl + letter = control codes 1 to 26.

SCREEN 13 tmp$ = " CHR$(###),\\,\ \ " tmp2$ = " CHR$(0) + CHR$(###) \ \" COLOR 14: LOCATE 3, 3: PRINT "The code can tell what key is pressed" COLOR 12: LOCATE 5, 14: PRINT CHR$(3); SPACE$(3); COLOR 13: PRINT CHR$(5); SPACE$(3); COLOR 12: PRINT CHR$(4); SPACE$(3); COLOR 13: PRINT CHR$(6) COLOR 10: LOCATE 7, 4: PRINT " Hit a key to find the ASCII Code" COLOR 5: LOCATE 13, 1: PRINT " Codes below 33 are called control keys" LOCATE 14, 1: PRINT " CHR$(0) + are 2 byte Extended key codes" COLOR 13: LOCATE 16, 1: PRINT " Extended: Press Alt + numberpad: Enter" LOCATE 18, 1: PRINT " Try some Ctrl, Alt, or Shift Combo's" COLOR 5: LOCATE 20, 1: PRINT " INKEY$ is used to detect the key entry" COLOR 2: LOCATE 22, 15: PRINT CHR$(1); " "; CHR$(2) COLOR 4: LOCATE 24, 10: PRINT "To Quit hit the TAB key"; COLOR 9 DO DO: SLEEP: A$ = INKEY$: LOOP UNTIL A$ <> "" 'legal ASC read keys IF ASC(A$) > 0 THEN ' normal key codes code% = ASC(A$) SELECT CASE code% CASE 7: Key$ = "Beep" CASE 8: Key$ = "Backspace" CASE 9: Key$ = "Tab Key" CASE 10: Key$ = "Line Feed" CASE 12: Key$ = "Form Feed" CASE 13: Key$ = "Enter" CASE 27: Key$ = "Escape" CASE 32: Key$ = "Space Bar" CASE 48 TO 57: Key$ = "Number" CASE 65 TO 90: Key$ = "Uppercase" CASE 97 TO 122: Key$ = "Lowercase" CASE ELSE: Key$ = "" END SELECT SELECT CASE code% 'check for unprintable control combo characters CASE 10 TO 13: Kcode% = 32 CASE ELSE: Kcode% = code% END SELECT COLOR 9: LOCATE 10, 5: PRINT USING tmp$; code%; CHR$(Kcode%); Key$; END IF IF ASC(A$) = 0 THEN 'two byte key codes code% = ASC(RIGHT$(A$, 1)) 'QBasic code 'code% = ASC(A$, 2) 'QB64 code alternative SELECT CASE code% CASE 16 TO 50: Key$ = "Alt+ letter" CASE 72: Key$ = CHR$(24) + " Arrow" CASE 75: Key$ = CHR$(27) + " Arrow" CASE 77: Key$ = CHR$(26) + " Arrow" CASE 80: Key$ = CHR$(25) + " Arrow" CASE 83: Key$ = "Delete" CASE 59: Key$ = "F1" CASE 60: Key$ = "F2" CASE 61: Key$ = "F3" CASE 62: Key$ = "F4" CASE 63: Key$ = "F5" CASE 64: Key$ = "F6" CASE 65: Key$ = "F7" CASE 66: Key$ = "F8" CASE 67: Key$ = "F9" CASE 68: Key$ = "F10" CASE 71: Key$ = "Home" CASE 73: Key$ = "Page " + CHR$(24) CASE 79: Key$ = "End" CASE 81: Key$ = "Page " + CHR$(25) CASE 82: Key$ = "Insert" CASE 83: Key$ = "Delete" CASE 84 TO 93: Key$ = "Shift+ F" CASE 94 TO 103: Key$ = "Ctrl+ F" CASE 104 TO 113: Key$ = "Alt+ F" CASE 114 TO 119: Key$ = "Ctrl + pad" CASE 120 TO 129: Key$ = "Alt+ number" CASE 132: Key$ = "Ctrl + pad" CASE 133: Key$ = "F11" CASE 134: Key$ = "F12" CASE 135: Key$ = "Shift+ F11" CASE 136: Key$ = "Shift+ F12" CASE 137: Key$ = "Ctrl+ F11" CASE 138: Key$ = "Ctrl+ F12" CASE 139: Key$ = "Alt+ F11" CASE 140: Key$ = "Alt+ F12" CASE ELSE: Key$ = "" END SELECT LOCATE 10, 5: PRINT USING tmp2$; code%; Key$ END IF LOOP UNTIL A$ = CHR$(9) SOUND 400, 4 SLEEP 3 SYSTEM

Explanation: The routine checks for a keypress and SLEEP guarantees that ASC will never read an empty string from INKEY$. When the keypress is determined to be two bytes (ASC(A$) = 0) the second SELECT CASE routine is used. You can even display non-keyboard extended characters. Just press Alt + numberpad code, release and press enter.

Note: Ctrl + letter keys will list the contol keys as normal codes. EX: Ctrl + G will BEEP (CHR$(7)).

(Return to Table of Contents)

References

See also:


Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Personal tools