Article
Storing text as numbers
You type "Hi" and send it to a friend. But a computer has only bits, and bits are 0s and 1s. How can a letter be a bit pattern?
The answer is a deal that everyone agrees on: every character gets its own number. The computer stores that number in binary.
A number for every character
A character is one letter, digit, space or symbol. A list that gives each character a number is called a character set. Here is a small part of the most famous one, ASCII. Read the table. The binary column uses 8 bits for each code.
| Character | Code | Binary |
|---|---|---|
| space | 32 | 00100000 |
| ! | 33 | 00100001 |
| 0 | 48 | 00110000 |
| 1 | 49 | 00110001 |
| A | 65 | 01000001 |
| B | 66 | 01000010 |
| C | 67 | 01000011 |
| a | 97 | 01100001 |
| b | 98 | 01100010 |
Notice three things:
- Letters follow the alphabet. A is 65, so B is 66, C is 67 and so on.
- A and a are different characters, so look at their codes.
- The digit characters start at 48. The character
0and the number 0 are not the same thing.
ASCII uses 7 bits for each character. So it can make 2^7 = 128 codes. That is enough for English letters, digits, punctuation and some control codes. In practice each code sits in one byte, so a short English message uses one byte for every character.
Check your understanding
In ASCII, the letter A has the code 65 and the letter a has the code 97. What does this show?
Unicode: room for the world
128 codes are not enough. Think of Hindi, Chinese, Arabic, Greek, and all the emoji. A bigger character set called Unicode was made for this. It gives a number to characters from almost every writing system, and to emoji too. The first 128 Unicode numbers are the same as ASCII, so old English text still works.
| Character | Unicode number |
|---|---|
| A | 65 |
| π | 960 |
| 你 | 20320 |
| 😀 | 128512 |
A number like 128512 does not fit in one byte, so Unicode text needs more bits for some characters. That is the price of having room for more than a million possible characters.
Check your understanding
Why can Unicode store emoji, while 7-bit ASCII cannot?
Worked example
Store the word "Hi" in ASCII with 8 bits for each character. The picture shows the journey, and the steps below work it out.
From a word to bits
Text
Hilook up each characterCharacter codes
H = 72, i = 105
write each in binaryStored bits
01001000 01101001
- H has the code 72. In binary, 72 = 64 + 8, so the code is
01001000. - i has the code 105. In binary, 105 = 64 + 32 + 8 + 1, so the code is
01101001. - Put the two codes next to each other, in the same order as the letters:
01001000 01101001.
The word has 2 characters and uses 2 bytes, which is 16 bits. To read it back, the computer cuts the bits into groups of 8, turns each group into a number, and looks up the character.
Common mistakes
- Mixing a digit character with a number. The character
5is stored with its character code. It is not stored as the binary number for 5. - Forgetting that the case matters. A capital letter and a small letter have different codes.
- Forgetting spaces. A space is a character too, with its own code, and it uses bits.
- Thinking a code is the letter's place in the alphabet. B is the second letter, but its ASCII code is 66, not 2.
On the exam
You will not be asked to remember the whole ASCII table. If a question needs a code, it gives you the code. Be ready to say that text is stored as numbers, that a character set such as ASCII or Unicode decides which number goes with which character, and that more characters need more bits.
Key words
Character: one letter, digit, space or symbol in a text.
ASCII: a character set that uses 7 bits per character, so it has 128 codes.
Unicode: a much larger character set, with room for characters from almost every language and for emoji.
Character set: an agreed list that links every character to a number.