The problem: a letter is a shape, not a number
A computer can only store bits. The letter H is a shape on a screen. So before a computer can store H, somebody has to decide a number for it.
Give every character a number. H = 72. i = 105. Space = 32. That number is the character code.
Store the number in binary. 72 becomes 01001000. You already know how to do that from Topic 3.
Write the list down and share it. The full list of characters and their numbers is a character set. It is a lookup table: find the character, read off its number. Or the other way round.
Why must everyone agree? The bits 01001000 mean nothing on their own. They only become "H" when the reader uses the same table as the writer. So the whole world needs to share one table. That shared table is the character set.
Morse code is a character set too. Everyone agreed that · · · · means H. If you invented your own dots, nobody could read your messages.
ASCII: the first big agreement
ASCII (said "ass-key") is the American Standard Code for Information Interchange. It was first published in 1963. Lower case letters were added in 1967.
7 bits per character. 7 bits make 2⁷ = 128 patterns, so ASCII has 128 characters, numbered 0 to 127.
What's in it: A–Z, a–z, the digits 0–9, punctuation, the space, and 33 invisible control characters.
Control characters are instructions, not shapes. 10 = new line. 9 = tab. 8 = backspace. 7 = ring a bell (old teleprinters had real bells).
Stored in a byte. Computers store data in whole bytes, so each 7-bit code sits in 8 bits with a 0 in front: A = 01000001.
| Character | Code | Binary (8 bits) | Hex |
|---|---|---|---|
| A | 65 | 01000001 | 41 |
| B | 66 | 01000010 | 42 |
| Z | 90 | 01011010 | 5A |
| a | 97 | 01100001 | 61 |
| 0 (the digit) | 48 | 00110000 | 30 |
| space | 32 | 00100000 | 20 |
| new line | 10 | 00001010 | 0A |
Why 7 bits gives exactly 128
It's the patterns rule from Topic 2. Each extra bit doubles the number of patterns. So the number of characters a set can hold is 2 to the power of the bits.
1 bit → 2 patterns. 2 bits → 4. 3 bits → 8. 4 bits → 16.
5 bits → 32. Enough for A–Z (26), but not lower case too.
6 bits → 64. Enough for A–Z, a–z (52) and digits (10) = 62. No room for punctuation.
7 bits → 128. Room for everything English needs (about 95 visible characters) plus 33 control codes.
8 bits → 256. Extended ASCII used the extra 128 for é, ñ, £ and box-drawing shapes (Step 6).
Why not 8 bits from the start? In the 1960s every bit cost money to store and send. 7 bits was the fewest that fitted English. The spare 8th bit was often used as a parity bit, an extra bit for spotting errors on noisy phone lines.
The clever layout: one bit changes the case
ASCII's order was chosen with care. Capitals start at 65. Lower case starts at 97. The gap is exactly 32, and 32 is one binary column.
Case flipper
Tap the 32s bit to switch case. Then try tapping other bits: watch the letter change. The 128s bit is greyed out because ASCII only uses 7 bits.
Letters run in order. A = 65, B = 66, C = 67 … Z = 90. So if you know one code you can count to any other letter. D = 65 + 3 = 68.
That's how computers sort words. They compare the codes. "cat" comes before "dog" because 99 < 100.
The 32 trick. Lower case = capital + 32. In binary that means: switch the 32s bit on. One tiny circuit can change case.
Capitals come first. Every capital (65–90) is smaller than every lower case letter (97–122). So a plain computer sort puts "Zebra" before "apple", because 90 < 97. Real programs convert everything to one case before sorting.
ord("A") gives the code: 65. chr(97) gives the character: a. Try it in Thonny.
print(ord("A")) # 65
print(chr(97)) # a
print(chr(ord("g") - 32)) # G: take the 32 away
print("Zebra" < "apple") # True! Z (90) is smaller than a (97)
The character '7' is not the number 7
The digit you type on a keyboard is a character, like a letter. Its code is not its value. The character '7' has code 55.
| Character | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
|---|---|---|---|---|---|---|---|---|---|---|
| Code | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 |
| Code − 48 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
You type 7 on the keyboard. The computer receives the character '7', code 55.
To get the number, take away the code of '0': 55 − 48 = 7.
That's the trick: '7' − '0' = 7. It works for every digit, because the digits run in order.
In Python,
int("7")does this for you. So doesord("7") - ord("0").
print("7" + "7")Joining two characters: "7" then "7".
print(7 + 7)
Adding two values.
A phone number like 0034 600 123 456 should be stored as text, not a number. As a number, the leading 0s would vanish and the spaces would break it. You never do maths on a phone number.
Three anchors give you everything: '0' = 48, A = 65, a = 97. Count on from these. Exams never expect you to memorise more.
Storing a word, and how big it is
To store text, the computer looks up each character, writes its code in binary, and puts the bytes one after another. Spaces and punctuation are characters too.
The word is Hi. Look up each character: H → 72, i → 105.
72 in binary: 64 + 8 = 72 → 01001000.
105 in binary: 64 + 32 + 8 + 1 = 105 → 01101001.
The computer stores the bytes side by side: 01001000 01101001.
Size: 2 characters × 8 bits = 16 bits = 2 bytes.
Secret message decoder
A friend sent you some bytes. Decode them one at a time: binary → denary → look up the character.
The text-size formula
Every character takes the same number of bits in ASCII. So the size is just characters × bits each.
Count every character. "Hello world" = H, e, l, l, o, space, w, o, r, l, d = 11. The space counts.
Bits each: 7 for pure ASCII, 8 when stored in bytes (extended ASCII, and how files really store ASCII).
Multiply: 11 × 8 = 88 bits.
Into bytes: 88 ÷ 8 = 11 bytes. With 8-bit codes, bytes = characters. Handy!
7-bit version: 11 × 7 = 77 bits. Exams sometimes ask this to check you know ASCII is 7-bit.
A text message of 160 characters in 7-bit codes: 160 × 7 = 1,120 bits = exactly 140 bytes. That's why old SMS messages were limited to 160 characters: the network allowed 140 bytes.
A new line is a character too (code 10). Windows files use two characters for each new line: 13 then 10. So the same text file can be a few bytes bigger on Windows.
Beyond English: ñ, á and the 8th bit
Look at the ASCII map again. There's no ñ, no á, no ¿, no €. 7-bit ASCII was made for American English. Spanish can't be written in it.
The fix: use the 8th bit. 8 bits give 256 codes. 0–127 stay ASCII. 128–255 are 128 new spaces. This is extended ASCII.
The new problem: there was no single "extended ASCII". Every region filled 128–255 with different characters. Each version is called a code page.
So one byte had many meanings. Byte 241 is ñ in Western Europe, ± on an old IBM PC, and ё in Russia.
| Spanish character | In 7-bit ASCII? | Latin-1 code | Unicode code point |
|---|---|---|---|
| n | Yes | 110 | U+006E |
| ñ | No | 241 | U+00F1 |
| Ñ | No | 209 | U+00D1 |
| á | No | 225 | U+00E1 |
| é | No | 233 | U+00E9 |
| ¿ | No | 191 | U+00BF |
| ¡ | No | 161 | U+00A1 |
Why not just make the table bigger? 256 slots can never hold Spanish, Greek, Arabic, Hindi, Chinese and Japanese at once. Chinese alone uses thousands of characters. One byte per character was simply too small for the world. That's the job Unicode took on.
Unicode: one table for the whole world
Unicode is one giant character set for every writing system, plus symbols and emoji. Version 1.0 came out in 1991. A new version comes out every year.
Every character gets one number, called its code point. It's written U+ followed by hex: ñ is U+00F1.
Hex, from Topic 3. F1 in hex = 15 × 16 + 1 = 241. Same number as Latin-1. Unicode copied Latin-1 for its first 256 codes.
The first 128 are exactly ASCII. A is still 65 (U+0041). Old English text is already valid Unicode.
Room for 1,114,112 code points (U+0000 to U+10FFFF). About 160,000 are used so far. Lots of room left.
| Character | What it is | Code point | Denary |
|---|---|---|---|
| A | Latin capital A (same as ASCII) | U+0041 | 65 |
| ñ | Spanish eñe | U+00F1 | 241 |
| Ω | Greek capital omega | U+03A9 | 937 |
| ب | Arabic letter beh | U+0628 | 1,576 |
| € | Euro sign | U+20AC | 8,364 |
| 你 | Chinese "you" | U+4F60 | 20,320 |
| 😀 | Grinning face emoji | U+1F600 | 128,512 |
Code point = the character's number (which row of the table). Encoding = how that number is written into bytes. Unicode gives the numbers. UTF-8 (next step) is the most common way to store them.
How many bits would a fixed size need?
The biggest code point is U+10FFFF = 1,114,111. Using the patterns rule: 2²⁰ = 1,048,576 is too small. 2²¹ = 2,097,152 is enough. So a code point needs 21 bits. Computers like whole bytes, so a fixed-size version (UTF-32) uses 4 bytes for every character.
4 bytes for every letter would make plain English files 4 times bigger than ASCII. "Hello" would take 20 bytes instead of 5. That waste is why UTF-8 was invented.
🇪🇸 (the Spanish flag) is two code points: the letters E and S in a special "regional indicator" range. 👍🏽 is a thumbs up plus a skin-tone code point. One picture on screen can be 2 to 7 code points underneath.
UTF-8: 1 to 4 bytes, as needed
UTF-8 stores small code points in 1 byte and bigger ones in 2, 3 or 4. English text stays as small as ASCII. Every other script still fits.
Starts with 0? It's a 1-byte character: plain ASCII. So ASCII files are already UTF-8.
Starts with 110, 1110 or 11110? It's the first byte of a 2, 3 or 4-byte character. Count the 1s at the start: that's how many bytes.
Starts with 10? It's a continuation byte: the middle or end of a longer character. Never a start.
Why this design is clever: if a byte is lost, the reader skips forward to the next byte that doesn't start with 10, and carries on. One damaged character, not a ruined file.
UTF-8 encoder, bit by bit
Pick or type a character. Press Next to encode it the way the computer does.
Ken Thompson and Rob Pike designed UTF-8 in 1992, sketching it on a placemat in a diner in New Jersey. Today about 98% of websites use it.
How UTF-8 changes the size of text
In ASCII, size = characters × 8 bits. In UTF-8 characters have different sizes, so you add them up instead.
| Text | Characters | 8-bit ASCII | UTF-8 | UTF-32 (4 bytes each) |
|---|---|---|---|---|
| Hola | 4 | 4 bytes | 4 × 1 = 4 bytes | 16 bytes |
| España | 6 | can't (no ñ) | 5 × 1 + 1 × 2 = 7 bytes | 24 bytes |
| 3 € | 3 | can't (no €) | 2 × 1 + 1 × 3 = 5 bytes | 12 bytes |
| 你好 | 2 | can't | 2 × 3 = 6 bytes | 8 bytes |
| 😀 | 1 | can't | 1 × 4 = 4 bytes | 4 bytes |
Text: España. Split it into characters: E, s, p, a, ñ, a.
For each one, find its code point and its UTF-8 size. E, s, p, a, a are ASCII (under 128): 1 byte each.
ñ is 241. That's above 127 but below 2,048: 2 bytes.
Add: 1 + 1 + 1 + 1 + 2 + 1 = 7 bytes = 7 × 8 = 56 bits.
Compare: if the formula S = c × b were used with b = 8, you'd say 6 bytes. It's 1 byte short, because ñ needs 2.
Same text, four encodings
For exam questions, use S = c × b with b = 7 or 8 (ASCII) or the bits the question gives you (for example "16-bit Unicode"). Real UTF-8 sizes depend on which characters are in the text.
| ASCII | Unicode (UTF-8) | |
|---|---|---|
| Characters | 128 (256 extended) | Over a million possible |
| Bits per character | 7 (stored in 8) | 8, 16, 24 or 32 |
| Languages | English only | All of them, plus emoji |
| File size | Smaller | Same for English, bigger for other scripts |
Letters in lights, and what the exams want
One byte, eight LEDs. Light the LEDs for a character's code and you can see a letter in binary. Flip just the 32s LED and the case changes.
Show A (65): only the 64s and 1s LEDs light. Now add the 32s LED: 97 = a. Students see Step 3's trick in real light.
| Exam board | What you must know |
|---|---|
| AQA 8525 | What a character set is. Convert characters ↔ codes. Codes are grouped and run in order (if A = 65, then C = 67). ASCII is 7-bit; Unicode uses more bits and covers far more characters; Unicode's first codes match ASCII. |
| OCR J277 | Binary codes for characters. The term character set. The link between bits per character and how many characters fit (ASCII, extended ASCII, Unicode). |
| Both | You don't memorise the table: a question gives you one code to count from. UTF-8 bit patterns (Step 8) go beyond GCSE: it's for your own understanding. |
Common mix-ups
The mistakes nearly everyone makes the first time. Read the left side, then the right.
Computers store letters as tiny pictures.
They store a number for each letter. The font draws the picture when it's shown.
The character '7' is stored as 7.
'7' is code 55 (00110111). The number 7 is 00000111.
ASCII uses 8 bits, so it has 256 characters.
ASCII is 7-bit: 128 characters. Extended ASCII uses 8 bits for 256, and there were many different versions.
Spaces don't count in the file size.
A space is a character (code 32). It takes a byte like any letter.
Lower case letters come before capitals, like in the alphabet song.
Capitals are 65–90, lower case 97–122. Capitals come first.
Unicode always uses 16 bits per character.
Unicode is the table. Encodings store it: UTF-8 uses 1–4 bytes, UTF-16 uses 2 or 4, UTF-32 always 4.
"España" means the file is broken.
The bytes are fine. The program is reading UTF-8 with an old 8-bit table.
Check yourself
Quick questions first. Then endless practice: a wrong answer shows you the working, step by step.