How UTF-8 Encodes Unicode Code Points in Binary

UTF-8 is a variable-length character encoding system that translates Unicode code points into one to four 8-bit bytes. By using specific binary prefixes for each byte, UTF-8 preserves backward compatibility with standard 7-bit ASCII, prevents byte-synchronization errors, and efficiently handles the entire Unicode character space ranging from U+0000 to U+10FFFF.

The UTF-8 Binary Mapping Template

Unicode assigns every character a unique numerical value known as a code point, denoted as U+XXXX in hexadecimal format. UTF-8 divides these code points into four distinct numerical ranges. Each range corresponds to a fixed byte length determined by predefined binary prefix bits:

Code Point Range (Hex) Byte Length Byte 1 Byte 2 Byte 3 Byte 4 Usable Data Bits
U+0000 to U+007F 1 0xxxxxxx 7
U+0080 to U+07FF 2 110xxxxx 10xxxxxx 11
U+0800 to U+FFFF 3 1110xxxx 10xxxxxx 10xxxxxx 16
U+10000 to U+10FFFF 4 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 21

The x positions represent the actual binary bits of the Unicode code point (payload), while the non-x bits serve as structural markers governed by UTF-8 rules.

Prefix Rules and Byte Roles

The binary prefixes establish the role of each byte in a sequence:

The Mapping Process: Step-by-Step

To map any Unicode code point to UTF-8 binary, follow these steps:

  1. Convert to Binary: Express the hexadecimal code point as a raw binary integer.
  2. Select the Byte Structure: Determine which of the four ranges contains the code point.
  3. Distribute Bits: Insert the binary digits into the x slots of the selected template from right to left (least significant bit to most significant bit).
  4. Pad Remaining Slots: Fill any remaining unoccupied x bits on the far left of the payload with 0s.

Example: Encoding the Euro Sign (€ / U+20AC)

  1. Hex to Binary: U+20AC converted to binary is 0010 0000 1010 1100.
  2. Determine Range: U+20AC falls between U+0800 and U+FFFF, requiring a 3-byte template:
    1110xxxx 10xxxxxx 10xxxxxx
  3. Fill the Template:
    • Payload: 0010 0000 1010 1100 (16 bits)
    • Byte 1: 1110 + 0010 = 11100010 (0xE2)
    • Byte 2: 10 + 000010 = 10000010 (0x82)
    • Byte 3: 10 + 101100 = 10101100 (0xAC)
  4. Result: The UTF-8 binary representation is 11100010 10000010 10101100 (Hex: E2 82 AC).

Benefits of Binary Prefix Encoding

This structural design ensures that no valid UTF-8 sequence forms a subsequence of another valid sequence. Decoders can start reading at any random point in a byte stream and instantly identify whether a byte is a single-byte character, the beginning of a multi-byte character, or a continuation byte, enabling fast error recovery and stream resynchronization.