← Data representation

Text as numbers

Text is stored as a list of numbers, one for each character. Every character has its own code: A is 65, a is 97, a space is 32 and even the digit 7 has a code (55). Unicode gives codes to over a hundred thousand characters, including emoji.

Every message you send is stored as a list of numbers. What list is your name?

In Everything is numbers, the numbers 72, 105, 33 were read as the text Hi!. Text is stored that way: as a list of numbers, one for each character (a letter, digit, space or symbol). Each character's number is its code.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

How is CAB stored?

In text, **A** is stored as 65 and **B** as 66. How is the word **CAB** stored?

TryTry it on the board and compare the result with your prediction.

Try: type a word

Was your prediction right? Now try your name.

Try it yourselfYour edits are saved in this browser.

Type CAB in the box. What numbers are stored?

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: Hi 7!

Worked exampleBefore each step, say what you think comes next.
  1. H→72\text{H} \to 72Capital letters start at A = 65. H is the 8th letter: 65 + 7.

Now find lower case c.

Lower case letters start at a = 97. What number stores lower case **c**?

Sam stores the text "Year 7" as numbers. Click the line where the working first goes wrong.

Why do **A** and **a** have different codes?

ModifyChange the problem on purpose and see what happens.

Vary: change one character

Try it yourselfYour edits are saved in this browser.

Type cab in lower case. How did the codes change?

Use the board. This ticks when you get there.

Now type Cab: only the first letter is a capital.

Use the board. This ticks when you get there.

Type just the digit 7.

Use the board. This ticks when you get there.

Type two words with a space between them, such as hi there.

Use the board. This ticks when you get there.

Type an emoji or an accented letter such as é or ñ. What happens to the code?

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Codes

Capital letters run from A = 65. What is the code for **Z**?

Which list stores the text **Go 2**?

Your password is **cat** but you type **Cat**. Why does it fail?

What character has the code 97 + 25 = 122?

Review: a text file holds 72, 105. A paint program opens it. What does it show?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Change case with numbers

Stretch: the Caesar cipher in Secret messages adds the same number to every code. What would adding 1 to every code in HI make?

Try it yourselfYour edits are saved in this browser.
Need a hint?

Lower case is 32 more than its capital.

Another hint

H is 72, so h is 72 + 32.

Another hint

Change 72 to 104 and 73 to 105.

The board shows HI (72, 73). Change only the numbers so the text reads hi in lower case.

Use the board. This ticks when you get there.

For teachers

Students type words and read their character codes to see that text is a list of codes, one per character, including spaces, digits and lower case, and that Unicode gives codes to every character (AC9TDI8K03 node 2). About 35 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Do the Try board yourself: type a few words, then an emoji.

The one idea: text is a list of character codes, in order. The codes here are ASCII, which Unicode keeps for the first 128 characters; Unicode then gives codes to every script and to emoji. Bytes come up as "0 to 255 fits in one byte"; the binary lessons explain why.

Links: Secret messages (Code pathway) builds ciphers that shift these codes; this lesson is the theory those lessons assume.

Running the lesson

  1. Predict: students who chose 3, 1, 2 used alphabet positions; ask what A's code is.
  2. Try: have everyone type their first name and compare: whose code list is longest?
  3. Worked example: before the 7 line, ask what the digit's code will be.
  4. Vary: ask why the emoji's code is so big.
  5. Make: ask what rule they used to turn capitals into lower case.

The hard part (on purpose)

Students treat a digit in text as an amount (7 stored as 7). Have them type 7 on the board and read the code aloud before the Worked example.

When students get stuck

Stretch and support

Stretch: Look up the Unicode code for an emoji you like (or type it on the board). Why do emoji make text messages bigger?

Support: Give the code table: A = 65 … Z = 90, a = 97 … z = 122, 0 = 48 … 9 = 57, space = 32.

Where it leads

Next: Colour and images (each pixel is numbers).

Where it leads: Secret messages (shifting codes to make a cipher), and the binary lessons (why one byte holds 0 to 255).

Sources

What each step is for

Now you can
Next lesson3. Colour and imagesA picture on a screen is a grid of tiny squares called pixels, and each pixel is stored as three numbers: how much red, green and blue light, from 0 to 255. Light mixes differently from paint: red and green light make yellow. A vector picture is stored differently, as shapes to draw.