Overview
When you type a letter on a keyboard, the computer cannot store the shape of the letter. It can only store bits. So every character is given a number, and that number is stored in binary. The most widely used numbering scheme is called ASCII. In this worksheet you will look up ASCII codes and convert them to binary yourself, encode whole words, decode a hidden message, and find the patterns that make ASCII easy to work with. There is no programming in this worksheet. Work through it with a pen and paper, and use the Check Answers buttons to test yourself as you go.
Learning Objectives
By the end of this lab you will be able to:
- Explain what a character set is and why computers need one
- Look up the ASCII code for a character and convert it to 8-bit binary
- Encode a word or short message as a sequence of binary bytes
- Decode binary bytes back into text using the ASCII table
- Describe the patterns in ASCII for upper case, lower case and digits, and the limits of ASCII
Concept
A character set is an agreed list that gives every character (letters, digits, punctuation, the space and some control codes) its own number. As long as two computers use the same character set, the number 65 sent by one will be shown as A by the other.
ASCII (American Standard Code for Information Interchange) is a character set that uses 7 bits, giving 27 = 128 codes, numbered 0 to 127. Codes 0 to 31 are invisible control codes (such as new line and tab). Codes 32 to 126 are the printable characters you can see on a keyboard. In practice each character is stored in a full byte (8 bits), with the extra leftmost bit set to 0.
Turning text into binary is a two-step process for each character: look up its ASCII code in the table, then convert that code from denary to 8-bit binary. Decoding is the same two steps in reverse.
Tasks
Task 1 - Characters are numbers
Here is the table of printable ASCII characters. For each character it gives the ASCII code in denary (Den) and in hex. Keep this table open for the rest of the worksheet.
| Char | Den | Hex | Char | Den | Hex | Char | Den | Hex |
|---|---|---|---|---|---|---|---|---|
| space | 32 | 20 | @ | 64 | 40 | ` | 96 | 60 |
| ! | 33 | 21 | A | 65 | 41 | a | 97 | 61 |
| " | 34 | 22 | B | 66 | 42 | b | 98 | 62 |
| # | 35 | 23 | C | 67 | 43 | c | 99 | 63 |
| $ | 36 | 24 | D | 68 | 44 | d | 100 | 64 |
| % | 37 | 25 | E | 69 | 45 | e | 101 | 65 |
| & | 38 | 26 | F | 70 | 46 | f | 102 | 66 |
| ' | 39 | 27 | G | 71 | 47 | g | 103 | 67 |
| ( | 40 | 28 | H | 72 | 48 | h | 104 | 68 |
| ) | 41 | 29 | I | 73 | 49 | i | 105 | 69 |
| * | 42 | 2A | J | 74 | 4A | j | 106 | 6A |
| + | 43 | 2B | K | 75 | 4B | k | 107 | 6B |
| , | 44 | 2C | L | 76 | 4C | l | 108 | 6C |
| - | 45 | 2D | M | 77 | 4D | m | 109 | 6D |
| . | 46 | 2E | N | 78 | 4E | n | 110 | 6E |
| / | 47 | 2F | O | 79 | 4F | o | 111 | 6F |
| 0 | 48 | 30 | P | 80 | 50 | p | 112 | 70 |
| 1 | 49 | 31 | Q | 81 | 51 | q | 113 | 71 |
| 2 | 50 | 32 | R | 82 | 52 | r | 114 | 72 |
| 3 | 51 | 33 | S | 83 | 53 | s | 115 | 73 |
| 4 | 52 | 34 | T | 84 | 54 | t | 116 | 74 |
| 5 | 53 | 35 | U | 85 | 55 | u | 117 | 75 |
| 6 | 54 | 36 | V | 86 | 56 | v | 118 | 76 |
| 7 | 55 | 37 | W | 87 | 57 | w | 119 | 77 |
| 8 | 56 | 38 | X | 88 | 58 | x | 120 | 78 |
| 9 | 57 | 39 | Y | 89 | 59 | y | 121 | 79 |
| : | 58 | 3A | Z | 90 | 5A | z | 122 | 7A |
| ; | 59 | 3B | [ | 91 | 5B | { | 123 | 7B |
| < | 60 | 3C | \ | 92 | 5C | | | 124 | 7C |
| = | 61 | 3D | ] | 93 | 5D | } | 125 | 7D |
| > | 62 | 3E | ^ | 94 | 5E | ~ | 126 | 7E |
| ? | 63 | 3F | _ | 95 | 5F | DEL | 127 | 7F |
Look at the table and answer the questions below. You do not need to convert anything to binary yet.
The ASCII code for A is
The ASCII code for a is
The ASCII code for the digit 0 is
The ASCII code for a space is
Which character has ASCII code 72?
Which character has ASCII code 105?
Why does a space need its own ASCII code?
Task 2 - The conversion process
To turn a character into binary, follow these steps every time.
- Look up the character in the ASCII table to find its code in denary.
- Convert the denary code to 8-bit binary.
- Check your answer by adding the column values back up.
For step 2, use the 8-bit column values and ask, from the left, "does this column fit into what is left?".
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|
Because ASCII only goes up to 127, the 128 column will always be 0.
Worked example: the letter K
Step 1. Look up K in the table. Its code is 75.
Step 2. Convert 75 to binary.
Convert 75 128 no bit = 0 64 fits 75 - 64 = 11 bit = 1 32 no bit = 0 16 no bit = 0 8 fits 11 - 8 = 3 bit = 1 4 no bit = 0 2 fits 3 - 2 = 1 bit = 1 1 fits 1 - 1 = 0 bit = 1 Answer 01001011
Step 3. Check: 64 + 8 + 2 + 1 = 75. Correct, so K is stored as 01001011.
Another route via hex. If you have done the Binary to Hexadecimal worksheet, you can also use the hex column. K is hex 4B. Turn each hex digit into a nibble: 4 = 0100 and B = 1011, giving 0100 1011. Same answer. Use whichever method you find easier, and use the other one to check.
Task 3 - Convert single characters
Work out each of these on paper using the three steps from Task 2. Write the ASCII code, then the 8-bit binary. Only press Check Answers once you have done them all.
A has ASCII code which is in binary
M has ASCII code which is in binary
z has ASCII code which is in binary
5 has ASCII code which is in binary
? has ASCII code which is in binary
(space) has ASCII code which is in binary
Task 4 - Encode a word
A word or sentence is stored as one byte per character, one after another, in order. Here is how the word Cat is stored.
| Character | ASCII code (denary) | Binary (8 bits) |
|---|---|---|
| C | 67 | 01000011 |
| a | 97 | 01100001 |
| t | 116 | 01110100 |
In memory these three bytes sit side by side: 01000011 01100001 01110100
Notice that upper case C and lower case a come from different parts of the table. Case matters. Cat and cat are stored differently.
Now encode the word Data, one character at a time.
D has ASCII code which is in binary
a has ASCII code which is in binary
t has ASCII code which is in binary
a has ASCII code which is in binary
Now encode your own initials, including a full stop after each one (for example J.S.). Write the bytes on paper. You can check them with the encoder in Task 7.
Task 5 - Decode a hidden message
Decoding is the same process backwards. For each byte, convert the binary to denary by adding up the column values, then look up the character in the ASCII table.
Worked example. 0100 1111
64 + 8 + 4 + 2 + 1 = 79. Code 79 in the table is O.
Decode this four-character message.
0100 0010 0111 1001 0111 0100 0110 0101
Byte 1 01000010 is code which is the character
Byte 2 01111001 is code which is the character
Byte 3 01110100 is code which is the character
Byte 4 01100101 is code which is the character
The hidden word is
Look again at byte 1. Is the first letter of the hidden word upper case or lower case?
Task 6 - Spotting the patterns
ASCII was designed with some useful patterns. Once you know them you can work out many codes without the table.
Pattern 1. The alphabet is in order. A is 65, B is 66, and so on up to Z at 90. Lower case runs from a (97) to z (122). If you know A = 65 you can count on to any capital letter.
Pattern 2. Upper and lower case differ by 32. Compare these two rows.
| Char | Code | Binary |
|---|---|---|
| A | 65 | 01000001 |
| a | 97 | 01100001 |
The only difference is the bit in the 32 column. Switching that one bit switches between capital and small letters. This is how a computer can change the case of text very quickly.
Pattern 3. Digits are not stored as their own value. The character 7 is code 55, which is 0011 0111. The right-hand nibble is 0111, which is 7, and every digit character starts with 0011.
G is 01000111. Without the table, g in binary is
Q is 01010001. Without the table, q in binary is
Count on from A = 65. The code for J is
Using pattern 3, the character 9 in binary is
Using pattern 3, the character 2 in binary is
The character "7" is stored as 00110111, but the number 7 is 00000111. Why are they different, and why does it matter?
The character "7" is a symbol to be displayed, so it is stored using its ASCII code, 55. The number 7 is a value to do arithmetic with, so it is stored as ordinary binary. If a program reads a digit typed by the user, it receives the character code, and must convert it (for example by subtracting 48) before it can do maths with it. Treating "7" + "1" as text gives "71", not 8.
Task 7 - Check your work with the encoder
Now that you can do the conversion by hand, use the encoder below to check your answers. Type any text (up to 16 characters) and it shows each character, its ASCII code and its 8 bits, and then the full stream of bits as it would sit in memory.
- Type the initials you encoded in Task 4 and compare with your paper answers.
- Type
Catand thencat. Which bit changes? - Type a word with a space in it. What does the space become?
- Type a
£sign or an accented letter such asé. What happens?
Task 8 - How much space does text take?
Because each ASCII character is stored in one byte, working out storage is simple.
Remember that spaces and punctuation are characters too. Hello, world! has 13 characters, so it needs 104 bits, or 13 bytes.
Computer needs bits
Hi there needs bits
ACCA4021 rocks! needs bytes
A text message is stored in 1,280 bits. It contains ASCII characters
Task 9 - The limits of ASCII
ASCII was designed in the 1960s for English. With only 128 codes there is no room for characters such as £, é, ŵ, Chinese or Arabic script, or emoji. You saw this in Task 7, where the encoder could not convert them.
Extended ASCII used the spare eighth bit to add another 128 characters (codes 128 to 255), but different countries filled those codes with different characters, so text often came out garbled when it moved between computers.
The modern answer is Unicode, which gives a unique number to over 150,000 characters from almost every writing system. The most common way of storing Unicode, UTF-8, uses one byte for the first 128 characters and two to four bytes for everything else. Those first 128 characters are exactly the same as ASCII, so everything you have learned in this worksheet still applies to plain English text today.
How many different characters can standard 7-bit ASCII represent?
Why is ASCII not enough for a Welsh language website?
A file of plain English text is saved as UTF-8. How does it compare with the same text saved as ASCII?
Task 10 - Check your understanding
What is the ASCII code for the letter "E"?
Which byte stores the character "d"?
The byte 00100001 is decoded using ASCII. Which character is it?
What are the two steps to turn a character into binary?
Show your full working to convert the word "OK" into binary.
O has ASCII code 79. 79 = 64 + 8 + 4 + 2 + 1, so O = 01001111.
K has ASCII code 75. 75 = 64 + 8 + 2 + 1, so K = 01001011.
"OK" is stored as 01001111 01001011, which is 2 bytes or 16 bits.
Explain why two computers must agree on a character set before they can exchange text.
A computer only stores and sends numbers in binary. The character set is the agreement that says which number means which character. If the sender and receiver use different character sets, the same number would be shown as a different character, and the text would come out wrong or garbled.