Unit 1: Digital information · Storing text in binary

Article

Storing text as numbers

6 min read

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.

CharacterCodeBinary
space3200100000
!3300100001
04800110000
14900110001
A6501000001
B6601000010
C6701000011
a9701100001
b9801100010

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 0 and 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.

CharacterUnicode number
A65
π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

  1. Text

    Hi

    look up each character
  2. Character codes

    H = 72, i = 105

    write each in binary
  3. Stored bits

    01001000 01101001

Every character becomes a number, and every number becomes bits. Reading text back is the same journey in reverse.
  1. H has the code 72. In binary, 72 = 64 + 8, so the code is 01001000.
  2. i has the code 105. In binary, 105 = 64 + 32 + 8 + 1, so the code is 01101001.
  3. 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 5 is 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.