# Character encoding

> CIE A-Level Computer Science · 9618 (2022-2024)
> Source: https://www.owlsprep.com/study/cie-9618-u1-character-encoding/

This module explains how text characters are stored as binary data in computer systems. You will learn common encoding schemes (ASCII, extended ASCII, Unicode) and how to calculate storage requirements for text strings.

**Prerequisites:** [Binary and hexadecimal number representation](https://www.owlsprep.com/study/cie-9618-u1-binary-hexadecimal/)

## Learning objectives

- Explain how text characters are encoded as binary data
- Compare ASCII, extended ASCII and Unicode encoding schemes
- Calculate memory size for storing a given text string
- Identify common errors in character encoding calculations

## Principles of Character Encoding

All text displayed on a computer is stored as binary data. A character set is a collection of characters that a system can encode, including letters, numbers, symbols, and control characters. Each character in the set is assigned a unique binary number.

**Character encoding** — The process of mapping each character in a character set to a unique binary value, so it can be stored or transmitted digitally.

*Example:* The character 'A' is mapped to $01000001$ in ASCII.

**Worked example:** A character set uses 7 bits per character. How many unique characters can this set encode?

1. Each bit can hold 2 possible values (0 or 1). For $n$ bits, the number of unique combinations is $2^n$.
2. Substitute $n = 7$:
3. $$2^7 = 128$$
4. A 7-bit character set can encode 128 unique characters.

**Exam command terms**

- **Calculate** — Show all working steps to get a numerical answer *(You will lose marks if you only write the final answer with no working)*

> **Exam tip:** Always remember that the number of unique characters is $2^n$, not $2n$, for $n$ bits per character.

## ASCII and Extended ASCII

ASCII (American Standard Code for Information Interchange) is the most widely used early character encoding, originally designed for English language communication over early computer networks.

**7-bit ASCII** — A 7-bit encoding scheme that supports 128 characters, including upper/lowercase English letters, digits, punctuation, and control characters (e.g., line feed, backspace).

7 bits fit into an 8-bit byte, with one extra bit originally used for parity error checking. This bit was later repurposed to create extended ASCII.

**Extended ASCII** — An 8-bit encoding that extends 7-bit ASCII to $2^8 = 256$ unique characters, adding extra symbols and accented characters for Western European languages.

**Worked example:** What is the minimum number of bytes required to store the string "HELLO CIE" using extended ASCII?

1. Extended ASCII uses 1 byte (8 bits) per character. Count all characters, including spaces.
2. Count characters: H, E, L, L, O, (space), C, I, E = 9 total characters.
3. $$9 \times 1 = 9 \text{ bytes}$$
4. The string requires 9 bytes of storage.

## Unicode

ASCII and extended ASCII only support a small number of characters, which is insufficient for non-English languages, emojis, and special academic or technical symbols. Unicode was developed to solve this problem.

**Unicode** — A universal character encoding standard designed to support all written languages, symbols, and emojis, with over 149,000 defined characters to date.

Unicode has multiple encoding formats (called Unicode Transformation Formats, or UTF) that vary in bit length per character:

- **UTF-8**: Variable-length encoding, uses 1-4 bytes per character. Backwards compatible with 7-bit ASCII, and the most common encoding for the World Wide Web.
- **UTF-16**: Uses 2 or 4 bytes per character, used for internal text storage in many operating systems.
- **UTF-32**: Fixed 4 bytes per character, simple but inefficient for most everyday use.

**Worked example:** Compare the storage of the string "Apple 🍎" in UTF-8 versus extended ASCII. Explain why extended ASCII cannot display the emoji.

1. Count total characters: A, p, p, l, e, (space), 🍎 = 7 characters.
2. Extended ASCII only has 256 character codes, and no code exists for the emoji. It can only store the first 6 characters ("Apple ") and will display a replacement error character. Total size = 6 × 1 byte = 6 bytes.
3. In UTF-8, all ASCII characters (including the space) use 1 byte each, and the emoji uses 4 bytes.
4. $$(6 \times 1) + (1 \times 4) = 10 \text{ total bytes}$$
5. UTF-8 correctly encodes all 7 characters in 10 bytes, while extended ASCII cannot encode the emoji.

## Calculating Text Storage Requirements

A common 3-5 mark exam question asks you to calculate the total memory size required to store a given block of text. The method follows a simple consistent process, regardless of the encoding.

> **tip**
>
> Always count spaces and punctuation as characters! They are encoded just like letters, so they add to the total size.

**Worked example:** A 100-page book uses 16-bit fixed-length Unicode encoding. If each page has 300 average words, each with 5 characters plus 1 space, calculate the total size in kilobytes (1 KB = 1000 bytes).

1. Calculate average characters per page: 5 characters + 1 space = 6 characters per word, 300 words per page = 300 × 6 = 1800 characters per page.
2. 16-bit fixed encoding means 16 bits = 2 bytes per character.
3. Total characters for 100 pages: 100 × 1800 = 180,000 characters.
4. $$\text{Total size} = 180000 \times 2 = 360000 \text{ bytes} = 360 \text{ KB}$$

**Check your understanding**

Check your understanding:

1. How many unique characters can be encoded with 10 bits per character?

   - 1024
   - 512
   - 10
   - 20

   *Why:* Correct: $2^{10} = 1024$ unique combinations.

## Common pitfalls

- **Wrong:** Forgetting to count spaces and punctuation when calculating total storage
  - Why it fails: Most students only count visible letters, but all characters require encoding
  - Correct: Always count every character in the string, including whitespace and punctuation
- **Wrong:** Calculating number of unique characters as $2n$ instead of $2^n$
  - Why it fails: Confusion between number of bits and number of possible binary combinations
  - Correct: Remember that $n$ bits give $2^n$ unique combinations, so always use exponentiation
- **Wrong:** Assuming all Unicode is fixed length 2 bytes per character
  - Why it fails: Only 16-bit fixed Unicode uses 2 bytes; most common Unicode formats are variable length
  - Correct: Always use the bit length specified in the question, do not assume a default value
- **Wrong:** Thinking extended ASCII is always compatible with Unicode
  - Why it fails: Only 7-bit ASCII is backwards compatible with UTF-8; extended ASCII uses non-standard extra codes that do not match Unicode
  - Correct: Remember that extended ASCII character values may map incorrectly to Unicode

## Cheatsheet

| Encoding | Bits per character | Max unique characters | Common use case |
| --- | --- | --- | --- |
| 7-bit ASCII | 7 bits | 128 | Basic English text |
| Extended ASCII | 8 bits | 256 | Western European languages |
| Fixed 16-bit Unicode | 16 bits | 65536 | Universal plain text |
| UTF-8 | 1-4 bytes (variable) | >149,000 | Web pages, backwards compatible with 7-bit ASCII |

## What's next

Character encoding is a core topic in information representation, and regularly appears as a short calculation question in Paper 1 of CIE 9618. Mastering the method for counting characters and calculating storage size is an easy way to secure marks in your exam. This concept underpins all work with text data, compression, and data transmission across networks. Next, you will move on to learning how other common types of media (images, audio) are represented as binary data in computer systems.

- [Data Compression](https://www.owlsprep.com/study/cie-9618-u1-data-compression/)
- [Binary representation of images](https://www.owlsprep.com/study/cie-9618-u1-binary-representation-of-images/)
- [Binary representation of sound](https://www.owlsprep.com/study/cie-9618-u1-binary-representation-of-sound/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u1-character-encoding/
