Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Character Encodings  ›  Capstone Project

UTF-8 — One to Four Bytes per Character

Unicode 12 minUTF-8 encodes each Unicode code point in one to four bytes, keeping ASCII characters as single bytes and marking longer sequences with special leading bits.

UTF-8 — One to Four Bytes per Character

Unicode gives 😀 the number 128,512, but a file is just bytes. Storing every character in 4 bytes would make English text four times bigger than ASCII. UTF-8 solves this elegantly: common characters take one byte, rarer ones take more. Today almost the entire web is UTF-8.

The rule

UTF-8 spends 1 to 4 bytes per code point; ASCII stays one byte.

The number of bytes depends only on how big the code point is:

Code point range Bytes Examples
U+0000 – U+007F 1 A, 7, space
U+0080 – U+07FF 2 é, ñ, Greek, Arabic
U+0800 – U+FFFF 3 अ, ₹, €, most Chinese characters
U+10000 – U+10FFFF 4 😀, rare historic scripts

The leading bits of every byte say what role it plays:

0xxxxxxx                              1-byte character (plain ASCII)
110xxxxx 10xxxxxx                     start of 2, then 1 continuation
1110xxxx 10xxxxxx 10xxxxxx            start of 3, then 2 continuations
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx   start of 4, then 3 continuations

This design gives UTF-8 its best properties. Any ASCII file is already valid UTF-8. A byte beginning 10 is never the start of a character, so a program dropped into the middle of a text can find the next character boundary. And because UTF-8 is read one byte at a time, it has no endianness problem.

When UTF-8 bytes are decoded with the wrong encoding, each multi-byte character turns into several junk symbols: café shows up as café. That garbling has a name, mojibake.

The shortcut

To count bytes, sort each character into its range: ASCII → 1, accented Latin and most other alphabets → 2, most Indian scripts, Chinese, Japanese, Korean and symbols like ₹ → 3, emoji → 4. Then add up.

Counting the bytes in "naïve €"Step 1 of 4
  1. Sort the characters

    n, a, v, e and the space are ASCII. ï is U+00EF. € is U+20AC.

Drills

Answer fast — trust the rule.

quick drill

How many bytes does the word "cat" take in UTF-8?

quick drill

How many bytes does é (U+00E9) take in UTF-8?

quick drill

How many bytes does 😀 (U+1F600) take in UTF-8?

quick drill

A UTF-8 byte in binary starts with the bits 10. What is it?

quick drill

How many bytes does "नमस्ते" take if it is 6 code points, all between U+0900 and U+097F?

Remember

  • UTF-8 uses 1 to 4 bytes: up to U+007F → 1, U+07FF → 2, U+FFFF → 3, beyond → 4.
  • ASCII text is already valid UTF-8, and continuation bytes always start with 10.
  • The trap: characters are not bytes, and decoding with the wrong encoding gives mojibake.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainCounting the bytes

Work out how many bytes the string 'Pay ₹500 😀' takes in UTF-8 (the characters are P, a, y, space, ₹, 5, 0, 0, space, 😀). Explain the rule you used, and explain what happens when these bytes are wrongly decoded as a one-byte-per-character encoding.