Year 8 · Representations · Part A: Storing information

Representing text

In one line

Every character gets a number. The number is stored in binary. The agreed list of characters and numbers is a character set.

1

Explain why every computer must use the same lookup table for characters.

2

Turn characters into ASCII codes and back, using the clever order (A = 65, a = 97, '0' = 48).

3

Work out how much space text takes, and how many bits a character set needs.

4

Say why Unicode exists, and how UTF-8 stores ñ, € or 😀 in 1 to 4 bytes.

Character setThe agreed table: each character and its number.
Character codeThe number for one character. A = 65.
ASCII7 bits. 128 characters. English only.
UnicodeOne table for every script on Earth, plus emoji.
Code pointA character's Unicode number, written U+ and hex: U+00F1 = ñ.
UTF-8The usual way to store Unicode: 1 to 4 bytes per character.

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.

  1. Give every character a number. H = 72. i = 105. Space = 32. That number is the character code.

  2. Store the number in binary. 72 becomes 01001000. You already know how to do that from Topic 3.

  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.

Both computers use the same table 1. You typeH 2. Look it upH → 72 3. Store / send01001000 4. Look it up72 → H 5. ScreenH ✓ The other computer uses a different table 1. You typeH 2. My tableH → 72 3. Store / send01001000 4. Your table72 → ç 5. Screenç ✗ The bits arrive perfectly. Only the table at the other end is different.
The bits are the same in both rows. The meaning comes from the table. A different table turns your message into nonsense.
?Why?

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.

e.g.Example

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.

  1. 7 bits per character. 7 bits make 2⁷ = 128 patterns, so ASCII has 128 characters, numbered 0 to 127.

  2. What's in it: A–Z, a–z, the digits 0–9, punctuation, the space, and 33 invisible control characters.

  3. Control characters are instructions, not shapes. 10 = new line. 9 = tab. 8 = backspace. 7 = ring a bell (old teleprinters had real bells).

  4. 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.

All 128 ASCII codes. Read a code as row + column: A is in row 64, column 1, so A = 64 + 1 = 65. Grey = control codes, green = digits, orange = capitals, teal = lower case.
control (invisible)space and punctuationdigitscapitalslower case
CharacterCodeBinary (8 bits)Hex
A650100000141
B660100001042
Z90010110105A
a970110000161
0 (the digit)480011000030
space320010000020
new line10000010100A

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. 1 bit → 2 patterns. 2 bits → 4. 3 bits → 8. 4 bits → 16.

  2. 5 bits → 32. Enough for A–Z (26), but not lower case too.

  3. 6 bits → 64. Enough for A–Z, a–z (52) and digits (10) = 62. No room for punctuation.

  4. 7 bits → 128. Room for everything English needs (about 95 visible characters) plus 33 control codes.

  5. 8 bits → 256. Extended ASCII used the extra 128 for é, ñ, £ and box-drawing shapes (Step 6).

?Why?

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.

Capital and lower case are twins. Only the 32s bit (yellow column) is different. Switch it on: add 32. Switch it off: take 32 away.
▶

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.

A
Code65

  1. 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.

  2. That's how computers sort words. They compare the codes. "cat" comes before "dog" because 99 < 100.

  3. The 32 trick. Lower case = capital + 32. In binary that means: switch the 32s bit on. One tiny circuit can change case.

!Watch out

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.

e.g.In Python

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.

The digits '0' to '9' sit at 48 to 57. Their right-hand 4 bits are the digit's value. The left-hand 0011 (= 48) just says "I'm a digit character".
Character'0''1''2''3''4''5''6''7''8''9'
Code48495051525354555657
Code − 480123456789
  1. You type 7 on the keyboard. The computer receives the character '7', code 55.

  2. To get the number, take away the code of '0': 55 − 48 = 7.

  3. That's the trick: '7' − '0' = 7. It works for every digit, because the digits run in order.

  4. In Python, int("7") does this for you. So does ord("7") - ord("0").

Characters (text)
print("7" + "7")
77

Joining two characters: "7" then "7".

Numbers
print(7 + 7)
14

Adding two values.

!Watch out

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.

★Remember

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.

  1. The word is Hi. Look up each character: H → 72, i → 105.

  2. 72 in binary: 64 + 8 = 72 → 01001000.

  3. 105 in binary: 64 + 32 + 8 + 1 = 105 → 01101001.

  4. The computer stores the bytes side by side: 01001000 01101001.

  5. 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.

Decoded so far

The text-size formula

Every character takes the same number of bits in ASCII. So the size is just characters × bits each.

  1. Count every character. "Hello world" = H, e, l, l, o, space, w, o, r, l, d = 11. The space counts.

  2. Bits each: 7 for pure ASCII, 8 when stored in bytes (extended ASCII, and how files really store ASCII).

  3. Multiply: 11 × 8 = 88 bits.

  4. Into bytes: 88 ÷ 8 = 11 bytes. With 8-bit codes, bytes = characters. Handy!

  5. 7-bit version: 11 × 7 = 77 bits. Exams sometimes ask this to check you know ASCII is 7-bit.

e.g.Example

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.

!Watch out

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.

  1. 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.

  2. The new problem: there was no single "extended ASCII". Every region filled 128–255 with different characters. Each version is called a code page.

  3. So one byte had many meanings. Byte 241 is ñ in Western Europe, ± on an old IBM PC, and ё in Russia.

One stored byte11110001= 241 = hex F1 Latin-1 (Western Europe)used in Spain, UK, France Code page 437 (IBM PC, 1981)the first PCs' table ISO 8859-5 (Cyrillic)used for Russian ñ ± ё
Same 8 bits, three different characters. If a Spanish email was opened on a Russian computer, every ñ turned into ё.
Spanish characterIn 7-bit ASCII?Latin-1 codeUnicode code point
nYes110U+006E
ñNo241U+00F1
ÑNo209U+00D1
áNo225U+00E1
éNo233U+00E9
¿No191U+00BF
¡No161U+00A1
?Why?

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.

  1. Every character gets one number, called its code point. It's written U+ followed by hex: ñ is U+00F1.

  2. 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.

  3. The first 128 are exactly ASCII. A is still 65 (U+0041). Old English text is already valid Unicode.

  4. Room for 1,114,112 code points (U+0000 to U+10FFFF). About 160,000 are used so far. Lots of room left.

CharacterWhat it isCode pointDenary
ALatin capital A (same as ASCII)U+004165
ñSpanish eñeU+00F1241
ΩGreek capital omegaU+03A9937
بArabic letter behU+06281,576
€Euro signU+20AC8,364
你Chinese "you"U+4F6020,320
😀Grinning face emojiU+1F600128,512
★Remember

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.

!Watch out

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.

e.g.Emoji are sometimes several code points

🇪🇸 (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.

Grey bits are markers: they say how long the character is. The coloured x bits hold the code point, cut into pieces. Each colour is one byte's share.
  1. Starts with 0? It's a 1-byte character: plain ASCII. So ASCII files are already UTF-8.

  2. 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.

  3. Starts with 10? It's a continuation byte: the middle or end of a longer character. Never a start.

  4. 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.

Why "España" happens Saved as UTF-8España ñ is stored as 2 bytes C3 11000011 B1 10110001 Opened as Latin-1España The old table reads one byte = one character. So the 2 bytes of ñ become 2 characters: C3 = 195 → ÃB1 = 177 → ± Fix: tell the program the file is UTF-8 (in HTML: <meta charset="utf-8">).
Nothing is broken in the file. The reader is just using the wrong table, like the bottom row in Step 1.
e.g.Fun fact

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.

TextCharacters8-bit ASCIIUTF-8UTF-32 (4 bytes each)
Hola44 bytes4 × 1 = 4 bytes16 bytes
España6can't (no ñ)5 × 1 + 1 × 2 = 7 bytes24 bytes
3 €3can't (no €)2 × 1 + 1 × 3 = 5 bytes12 bytes
你好2can't2 × 3 = 6 bytes8 bytes
😀1can't1 × 4 = 4 bytes4 bytes
  1. Text: España. Split it into characters: E, s, p, a, ñ, a.

  2. For each one, find its code point and its UTF-8 size. E, s, p, a, a are ASCII (under 128): 1 byte each.

  3. ñ is 241. That's above 127 but below 2,048: 2 bytes.

  4. Add: 1 + 1 + 1 + 1 + 2 + 1 = 7 bytes = 7 × 8 = 56 bits.

  5. 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

1 byte2 bytes3 bytes4 bytes
★Remember

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.

ASCIIUnicode (UTF-8)
Characters128 (256 extended)Over a million possible
Bits per character7 (stored in 8)8, 16, 24 or 32
LanguagesEnglish onlyAll of them, plus emoji
File sizeSmallerSame 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.

e.g.Try this with the build

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 boardWhat you must know
AQA 8525What 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 J277Binary codes for characters. The term character set. The link between bits per character and how many characters fit (ASCII, extended ASCII, Unicode).
BothYou 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.

People think

Computers store letters as tiny pictures.

Actually

They store a number for each letter. The font draws the picture when it's shown.

People think

The character '7' is stored as 7.

Actually

'7' is code 55 (00110111). The number 7 is 00000111.

People think

ASCII uses 8 bits, so it has 256 characters.

Actually

ASCII is 7-bit: 128 characters. Extended ASCII uses 8 bits for 256, and there were many different versions.

People think

Spaces don't count in the file size.

Actually

A space is a character (code 32). It takes a byte like any letter.

People think

Lower case letters come before capitals, like in the alphabet song.

Actually

Capitals are 65–90, lower case 97–122. Capitals come first.

People think

Unicode always uses 16 bits per character.

Actually

Unicode is the table. Encodings store it: UTF-8 uses 1–4 bytes, UTF-16 uses 2 or 4, UTF-32 always 4.

People think

"España" means the file is broken.

Actually

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.

[ {"q":"If A = 65, what is D?","o":["67","68","69"],"a":1,"w":"A 65, B 66, C 67, D 68. Letters run in order."}, {"q":"How many characters can 7-bit ASCII represent?","o":["127","128","256"],"a":1,"w":"2⁷ = 128 patterns, numbered 0 to 127."}, {"q":"'Cat' in 8-bit ASCII takes…","o":["3 bits","24 bits","8 bits"],"a":1,"w":"3 characters × 8 bits = 24 bits (3 bytes)."}, {"q":"A = 65 and a = 97. Which bit is different between them?","o":["The 1s bit","The 32s bit","The 128s bit"],"a":1,"w":"97 − 65 = 32. Only the **32s** column changes: 01**0**00001 → 01**1**00001."}, {"q":"What is the code of the character '7'?","o":["7","55","48"],"a":1,"w":"'0' is 48, so '7' is 48 + 7 = **55**."}, {"q":"To store emoji you need…","o":["ASCII","Extended ASCII","Unicode"],"a":2,"w":"ASCII has only 128 characters. Emoji live in Unicode."}, {"q":"In UTF-8, how many bytes does ñ (code point 241) take?","o":["1","2","4"],"a":1,"w":"241 is above 127 but below 2,048, so 2 bytes: 11000011 10110001."}, {"q":"A web page shows 'España'. Why?","o":["The file is broken","UTF-8 bytes are being read with an old 8-bit table","Spanish isn't in Unicode"],"a":1,"w":"ñ is 2 bytes in UTF-8. An old table reads each byte as its own character: à and ±."} ]