UTF-16 and UTF-32
You'll learn: How UTF-16 and UTF-32 differ from UTF-8 by using fixed or predictable character widths, and why that matters for storage and processing.
The Core Idea
While UTF-8 uses 1-4 bytes per character (variable-width), UTF-16 and UTF-32 take different approaches:
- UTF-32 uses exactly 4 bytes (32 bits) for every character—completely fixed-width
- UTF-16 uses 2 or 4 bytes (16 or 32 bits) per character—mostly fixed, but not always
Think of it like shipping boxes: UTF-8 uses boxes that shrink to fit (1-4 sizes), UTF-32 uses one giant box for everything, and UTF-16 uses medium boxes that occasionally need doubling up.
UTF-32: Simple but Spacious
Every character = 4 bytes. The letter "A" takes 4 bytes. An emoji takes 4 bytes. This makes finding the Nth character trivial (just jump to position N × 4), but wastes space for English text (4× larger than ASCII).
Example: "Hi" in UTF-32 = 8 bytes total
UTF-16: The Middle Ground
Most common characters (including English, Chinese, Arabic) fit in 2 bytes. Rarer symbols (some emoji, ancient scripts) need 4 bytes using "surrogate pairs." This balances space efficiency with simpler processing than UTF-8 for many languages.
Example: "Hi" in UTF-16 = 4 bytes total; "Hi😊" = 8 bytes (emoji needs the extra pair)
The Tradeoffs
- UTF-32: Easy indexing, huge files
- UTF-16: Good for non-English text, still variable-width
- UTF-8: Best for web/English, complex indexing
Key Takeaway: UTF-16 and UTF-32 trade storage efficiency for simpler character access—UTF-32 is fully fixed-width (4 bytes always), while UTF-16 mostly uses 2 bytes but sometimes 4, making both faster to navigate than UTF-8 but often larger in file size.