Lesson 4 of 5 · Open

Letters as numbers

How a computer stores text: every character gets a number.

You type Hi and send it. But inside the phone there are only bits, zeros and ones. Bits can write numbers, as the binary system shows. But a letter is not a number. So how does it get into memory?

The trick is a code: number all the letters. In computer science, every letter, every digit, and every punctuation mark is called a character. A table that gives every character a number is called a character code. What gets stored is not the letter but its number, as a binary number in bits.

The ASCII code

The best-known character code is called ASCII, pronounced ask-ee. It was set in the United States in the 1960s and has 128 characters with the codes 0 to 127: the English alphabet in uppercase and lowercase, the digits, punctuation marks, and a few control characters such as the line break, which show nothing. Even the largest code, 127, fits into one byte.

CharacterCodeCharacterCode
A65a97
B66b98
C67c99
Z90z122
048957
Space32.46

In between, they go in order. Uppercase letters run from A = 65 to Z = 90, lowercase letters from a = 97 to z = 122. You find a letter's code by counting on in the alphabet from A or a. A lowercase letter always has a code 32 greater than its uppercase letter. The digits 0 to 9 have the codes 48 to 57.

The space between two words is a character too, with the code 32. You cannot see it, but it takes up its place like any letter. In ASCII every character takes exactly one byte, the space included. So you find how many bytes a text takes by counting its characters, spaces and all.

New terms

character
a single letter, digit, punctuation mark, or space
character code
a table that gives every character a number
ASCII
a character code with 128 characters and the codes 0 to 127. Each character takes one byte
space
the invisible character between two words, 32 in ASCII

Example

Hi in numbers

What are the codes for Hi? Count on in the alphabet from A.

  1. H is 7 letters after A: 65 + 7 = 72
  2. Uppercase I is 8 after A: 65 + 8 = 73
  3. Lowercase i is 32 more: 73 + 32 = 105
  4. Hi becomes 72, 105. Two characters, two bytes in ASCII.

Quick check

Do A and a have the same code?

In memory, the code is stored as a binary number. The 65 for A is 64 + 1, so in binary it is 1000001. That is seven places, but a byte has eight bits. So a 0 goes in front: in memory, A is the bit sequence 01000001.

From codes back to text

Example

Reading six codes

An app has stored the codes 66, 117, 115, 32, 49, 50. What text is it? For each code, first check which part of the table it falls in.

  1. 66 is among the uppercase letters, 1 after A: B
  2. 117 and 115 are lowercase: 117 − 97 = 20, 115 − 97 = 18
  3. 20 after a is u, 18 after a is s.
  4. 32 is the space.
  5. 49 and 50 are the digits 1 and 2, because 0 has 48.
  6. The text is Bus 12: six characters, six bytes in ASCII.

Typical mistake

Storing the digit 7 in a text with the code 7. That cannot be right: the codes below 32 belong to control characters that show nothing. In a text, 7 is a character like any letter and has its own place in the table: 48 + 7 = 55.

More than 128 characters: Unicode

ASCII has no ä, ö, ü, or ß, let alone Chinese or Arabic script. That is why today there is Unicode: a character code for the scripts of the whole world, with well over 100,000 characters, emojis included. The first 128 codes are the same as in ASCII. So A is 65 in Unicode too.

That many codes do not fit into one byte. So in UTF-8, the form most used today to store Unicode, a character takes 1 to 4 bytes. ASCII characters take exactly 1 byte there, ä, ö, ü, and ß 2 bytes each, the euro sign 3, and most emojis 4. A text with umlauts therefore takes more bytes in UTF-8 than it has characters.

Remember

A character code gives every character a number, the space included. In ASCII, A is 65 and every character takes one byte.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Input

What is the ASCII code of uppercase E?

Enter the result.

E =

Task 2 · Input

The license plate DO KR 3150 is stored as text. How many bytes does it take in ASCII?

Enter the result.

bytes

Task 3 · Order

Order from the smallest to the largest ASCII code.

Drag the rows into the right order.

  1. 1Digit 3
  2. 2Space
  3. 3Uppercase T
  4. 4Uppercase M
  5. 5Lowercase k

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

Task 4 · Choice · optional

A message consists of the ASCII codes 83, 79, 83. What text is it?

One answer is right.

Task 5 · Input · optional

How many bytes does the German word Grüße take in UTF-8?

Enter the result.

bytes

Task 6 · Several right · optional

Which statements about character codes are true?

Several answers are right. Pick all of them.

Back to the chapter