Course contentsShow
← Computer Science
Lesson 27 of 2,8721. Digital FoundationsFree lesson

UTF-8 Encoding

Learning variable-length UTF-8 encoding: 1-4 bytes per character, backward compatible with ASCII.

UTF-8 Encoding

What you'll learn: How UTF-8 uses 1 to 4 bytes per character to encode the entire Unicode character set while staying compatible with ASCII.

Why Variable-Length Encoding?

Remember that Unicode (concept #26) assigns a unique number to every character from every writing system in the world. But how do we store these numbers as binary data? That's where UTF-8 comes in.

UTF-8 is a variable-length encoding scheme. Instead of using the same number of bytes for every character, it adapts:

  • 1 byte for common characters (0–127) — these are identical to ASCII
  • 2 bytes for characters like é, ñ, or Greek letters
  • 3 bytes for most other languages (Chinese, Arabic, emoji)
  • 4 bytes for rare characters and special symbols

Backward Compatibility with ASCII

Here's the brilliant part: any text that was valid ASCII (concept #24) is automatically valid UTF-8. If you have the letter A (ASCII value 65, binary 01000001), UTF-8 stores it exactly the same way — as one byte: 01000001.

This means old programs designed for ASCII can often read UTF-8 text without breaking, as long as they stick to basic English characters.

How It Works (Simple View)

UTF-8 uses special bit patterns to signal "this character continues into more bytes." Without getting into the technical details of those patterns yet, just know:

  • Small numbers (0-127): one byte, looks like ASCII
  • Larger numbers: multiple bytes work together to represent one character

Think of it like addresses: a local address needs only a street name, but an international address needs city, country, and postal code — more information takes more space.

Key Takeaway: UTF-8 encodes Unicode characters using 1 to 4 bytes depending on the character, staying 100% compatible with ASCII for English text while supporting every language in the world.