You've hit on a really common and good question! The name "UTF-8" is historically a bit misleading. Here's a breakdown of why it's called UTF-8, and why it doesn't always use 8 bits:
Understanding the History & Goals
- Unicode: The core idea is Unicode. Unicode aims to assign a unique number (a "code point") to every character in every language. These code points are typically represented as numbers like U+0041 (for the letter 'A').
- UTF (Unicode Transformation Format): Unicode itself is just a character set. It doesn't define how those code points are stored in a computer's memory or transmitted. That's where UTF comes in. UTFs are different encoding schemes for Unicode. They define how to convert those Unicode code points into sequences of bytes.
- Early Encoding Schemes: Before UTF-8, there were other Unicode encodings like UTF-16 and UCS-2. These often used fixed-width encodings (e.g., 16 bits per character). This was simpler, but less efficient for languages like English or Chinese where many common characters could be represented with fewer bits.
- The Need for Compatibility: One of the primary goals of UTF-8 was to be backward compatible with ASCII. ASCII is a 7-bit encoding that was the dominant standard for English text for decades.
Why "UTF-8" then?
- 8-bit Focus (Initially): The designers of UTF-8 wanted an encoding that could represent all Unicode characters, but also be efficient for ASCII characters. They chose to base it around 8-bit bytes.
- ASCII Compatibility: The first 128 code points in Unicode (U+0000 to U+007F) are exactly the same as ASCII. In UTF-8, these characters are represented using a single byte, and that byte is identical to the ASCII representation. This meant existing ASCII files were already valid UTF-8 files! This was a huge win for adoption.
- Variable Width: To represent characters outside the ASCII range (the vast majority of characters in other languages), UTF-8 uses a variable-width encoding. This means:
- Characters with code points 0-127 (ASCII) are encoded using 1 byte.
- Characters with code points 128-2047 are encoded using 2 bytes.
- Characters with code points 2048-65535 are encoded using 3 bytes.
- Characters with code points 65536-1114111 are encoded using 4 bytes.
So, it's not always 8 bits!
You're absolutely right to point this out. While UTF-8 is based on 8-bit bytes, it uses 1, 2, 3, or 4 bytes to represent a single character, depending on the character's Unicode code point.
The name stuck because of its historical roots and the crucial ASCII compatibility. It's a bit of a misnomer now, but it's the standard name and everyone understands what it means.
In summary:
- UTF-8 was designed to be a flexible encoding for Unicode.
- It prioritizes compatibility with ASCII (using 1 byte for ASCII characters).
- It uses a variable number of bytes (1-4) to represent all Unicode characters.
- The "8" in UTF-8 refers to its initial design based on 8-bit bytes, not that it always uses 8 bits.