ASCII
Categories: data representation text character
ASCII (the American Standard Code for Information Interchange) is a character set that uses numbers 32 to 127 to represent a set of the main characters used in English language text. These are:
- The upper case letters A to Z
- The lower case letters a to z
- The digits 0 to 9
- A selection of over 30 punctuation symbols including full stop, comma, brackets, quotes and more
- The Space character
ASCII was first developed in the 1960s, but relatively few people used it at the time. An alternative scheme, called EBCDIC, was more popular.
The first IBM PCs, created in the early 1980s, used ASCII, and its popularity grew from that.
ASCII Table
Here is a table of the 128 ACSII characters. The table shows the decimal (denary) value and the hexadecimal value for each character. For example character A has decimal value 65, which is equivalent to 0x41 in hexadecimal.
Structure of the table
The first 32 characters in the table (NUL through to US) are called control characters. Character 127 (DEL) is also a control character. You might recognise CR (carriage return) and LF (line feed), which are used to mark the end of each line in a text file, and the TAB character which is used to align text. Most of the other control characters are no longer really used very often.
Characters 32 to 126 are used for printable characters:
- 48 to 57 are used for the digits zero to nine (in hex these are values 0x30 to 0x39)
- 65 to 90 are the upper case letters (hex 0x41 to 0x5A)
- 97 to 122 are the lower case letters (hex 0x61 to 0x7A)
- The gaps are filled with punctuation characters
You can convert a digit to an ASCII code by adding 0x30 to its value. Eg number '4' has ASCII code 0x34.
You can convert an upper case letter to lower case by adding 0x20 to its value. Eg 'Q' has code 0x51, 'q' has code 0x71.
ASCII conversion
Converting a text string to ASCII values is a simple matter of looking up each character in the table. For example, to convert "Hello" to ASCII, do the following:
- Look up H in the table, it has a decimal value 72
- Look up e in the table, it has a decimal value 115
- and so on, to the end of the string
Take care to look up the upper or lower case letter correctly. You should find that the ASCII values for the string "Hello" are:
[72, 115, 108, 108, 111]
Converting ASCII codes back to a text string is just as easy - look up the letter corresponding to each number. For example, to convert:
[119, 111, 114, 108, 100]
Do the following
- Look up decimal 119 in the table, it corresponds to the letter w
- Look up decimal 114 in the table, it corresponds to the letter o
- etc
You will find the ASCII codes represent the string "world".
Using ASCII codes in a program
Most programming languages allow you to convert between ASCII values and text characters. For example in Python 3 you can use the chr()
function to convert a value to text. For example, this code prints B (the character with ASCII value 66):
c = chr(66) print(c)
This code prints 69 (the ASCII value of character 'D'):
c = ord('D') print(c)
Most languages have similar functions.
See also
- Binary
- Bits and bytes
- Units of storage
- Powers of two
- Representing numbers
- Binary numbers
- Hexadecimal numbers
- Adding binary numbers
- Negative binary numbers
- Bitwise logical operations
- Binary shift
- Character representation
- Extended ASCII
- Unicode
- Digital images
- Bitmap images
- Computer colour
- Colour depth
- Vector images
- Image file formats
- Computer sound
- Recording sound
- Playing sound
- Sound file formats
Sign up to the Creative Coding Newletter
Join my newsletter to receive occasional emails when new content is added, using the form below:
Popular tags
555 timer abstract data type abstraction addition algorithm and gate array ascii ascii85 base32 base64 battery binary binary encoding binary search bit block cipher block padding byte canvas colour coming soon computer music condition cryptographic attacks cryptography decomposition decryption deduplication dictionary attack encryption file server flash memory hard drive hashing hexadecimal hmac html image insertion sort ip address key derivation lamp linear search list mac mac address mesh network message authentication code music nand gate network storage none nor gate not gate op-amp or gate pixel private key python quantisation queue raid ram relational operator resources rgb rom search sort sound synthesis ssd star network supercollider svg switch symmetric encryption truth table turtle graphics yenc