Section 12.48

Unicode code points, scalar values, code units, and byte encodings

Definition

Unicode is the standard that gives each character of the world’s writing systems a number, called a code point, from U+0000 to U+10FFFF; U+ is followed by hexadecimal digits. A scalar value is any code point outside U+D800 to U+DFFF, a range reserved for UTF-16. An encoding form (UTF-8, UTF-16 or UTF-32, where UTF stands for Unicode Transformation Format) turns each scalar value into code units of 8, 16 or 32 bits, and an encoding scheme fixes their byte order §4.21. Unicode itself is therefore not a 16-bit encoding.

The idea

Computers store text as bytes, so programs that exchange text must agree on how characters become numbers and numbers become bytes. Before Unicode, each region used its own table. In Latin-1, the Western European table of ISO 8859-1 [511], the byte 0xE9 meant é, while other tables gave that byte other meanings, so one file showed different text on different machines. Unicode separates two questions. Which number is this character? That is the code point, the same everywhere. Which bytes carry that number? That is the encoding, and there are several.

The first design of Unicode assumed that 65,536 characters, the number that fits in 16 bits, would be enough, and systems of the 1990s such as Windows and Java adopted 16-bit characters. Unicode 2.0 (1996) [512] extended the code space to 17×65,536=1,114,11217 \times 65{,}536 = 1{,}114{,}112 code points and kept those systems working with surrogate pairs, two 16-bit units that together stand for one code point above U+FFFF.

How it works

Text passes through four layers, each with its own unit.

  1. Character and code point. The Unicode Standard assigns a character, such as latin small letter e with acute, a code point, here U+00E9, the number 14×16+9=23314 \times 16 + 9 = 233. The code space has 17 planes of 65,536 code points. Plane 0, U+0000 to U+FFFF, is the Basic Multilingual Plane; code points above it are called supplementary.

  2. Scalar value. The 2,048 code points U+D800 to U+DFFF are the surrogates. They exist only so that UTF-16 can form pairs, and they are never characters. The other 1,114,112−2,048=1,112,0641{,}114{,}112 - 2{,}048 = 1{,}112{,}064 code points are the scalar values, the numbers an encoding may carry; most are not yet assigned to any character.

  3. Encoding form and code unit. An encoding form maps each scalar value to code units of fixed width: one to four 8-bit units in UTF-8, one or two 16-bit units in UTF-16, and always one 32-bit unit in UTF-32.

  4. Encoding scheme and byte. A code unit wider than a byte must be written as bytes in some order. UTF-16BE and UTF-32BE (big-endian) write the most significant byte, the one holding the highest-value bits, first; UTF-16LE and UTF-32LE (little-endian) write it last. UTF-8 needs no such choice, because its code unit is a byte.

Decoding runs the layers backward, after a check that the units are well formed. The bytes alone do not say which encoding produced them; sender and receiver must agree on it.

Worked example

Three characters show every layer. Byte values are in hexadecimal.

  1. U+0041, latin capital letter a, is the number 4×16+1=654 \times 16 + 1 = 65. UTF-8 uses one byte, 41. UTF-16 uses one code unit, 0041, written as the bytes 00 41 in UTF-16BE and 41 00 in UTF-16LE. UTF-32 uses one code unit of four bytes, 00000041.

  2. U+00E9, é, is the number 233, in the two-byte range of UTF-8, whose pattern 110xxxxx 10xxxxxx has 5 payload bits in the first byte and 6 in the second. Since 233=3×64+41233 = 3 \times 64 + 41, the high 5 bits hold 3 (binary 00011) and the low 6 bits hold 41 (binary 101001), giving the bytes 11000011 = C3 and 10101001 = A9. A decoder strips the prefixes and computes 3×64+41=2333 \times 64 + 41 = 233. Decoded wrongly as Latin-1, the same bytes show two characters, Ã and ©.

  3. U+1F600, grinning face, an emoji, is the number 65,536+15×4096+6×256=128,51265{,}536 + 15 \times 4096 + 6 \times 256 = 128{,}512, above U+FFFF.

U+1F600 in UTF-16. Subtract 0x10000: u=128,512−65,536=62,976u = 128{,}512 - 65{,}536 = 62{,}976. Split uu into two 10-bit halves: 62,976=61×1024+51262{,}976 = 61 \times 1024 + 512, so the high half is 61 (0x3D) and the low half is 512 (0x200). The high surrogate is 𝟶𝚡𝙳𝟾𝟶𝟶+𝟶𝚡𝟹𝙳=𝟶𝚡𝙳𝟾𝟹𝙳\mathtt{0xD800} + \mathtt{0x3D} = \mathtt{0xD83D} and the low surrogate is 𝟶𝚡𝙳𝙲𝟶𝟶+𝟶𝚡𝟸𝟶𝟶=𝟶𝚡𝙳𝙴𝟶𝟶\mathtt{0xDC00} + \mathtt{0x200} = \mathtt{0xDE00}, so the bytes are D8 3D DE 00 in UTF-16BE and 3D D8 00 DE in UTF-16LE. Decoding reverses the steps: 61×1024+512+65,536=128,51261 \times 1024 + 512 + 65{,}536 = 128{,}512.

U+1F600 in UTF-8. The four-byte pattern carries 3, 6, 6 and 6 payload bits, so the place values of the four payloads are 643=262,14464^3 = 262{,}144, 642=409664^2 = 4096, 64 and 1. Since 128,512=0×262,144+31×4096+24×64+0128{,}512 = 0 \times 262{,}144 + 31 \times 4096 + 24 \times 64 + 0, the payloads are 0, 31, 24 and 0, and the bytes are 11110000 = F0, 10011111 = 9F, 10011000 = 98 and 10000000 = 80. One scalar value is thus 4 UTF-8 code units, 2 UTF-16 code units and 1 UTF-32 code unit.

Stated precisely

Sets. The code points are the integers 0 to 0x10FFFF, 1,114,112 values. The scalar values are the code points outside 0xD800 to 0xDFFF, 1,112,064 values. A sequence of code units is well formed when it is the encoding of some sequence of scalar values.

UTF-8, as RFC 3629 restricts it, uses the shortest of these patterns that holds the scalar value vv, with the bits of vv in the x positions, most significant first:

The first byte of each sequence says how many bytes follow, and every following byte, a continuation byte, starts with 10. A byte sequence is well-formed UTF-8 exactly when it splits into these patterns with no longer pattern than needed (no overlong form), no surrogate and no value above 0x10FFFF. As a consequence, the bytes C0, C1 and F5 to FF never occur in UTF-8.

UTF-16. A scalar value v≤𝟶𝚡𝙵𝙵𝙵𝙵v \le \mathtt{0xFFFF} is one code unit equal to vv. A scalar value v≥𝟶𝚡𝟷𝟶𝟶𝟶𝟶v \ge \mathtt{0x10000} becomes two code units. Let u=v−𝟶𝚡𝟷𝟶𝟶𝟶𝟶u = v - \mathtt{0x10000}, a number of at most 20 bits. The high surrogate is 𝟶𝚡𝙳𝟾𝟶𝟶+⌊u/1024⌋\mathtt{0xD800} + \lfloor u / 1024 \rfloor and the low surrogate is 𝟶𝚡𝙳𝙲𝟶𝟶+(umod1024)\mathtt{0xDC00} + (u \bmod 1024), where ⌊x⌋\lfloor x \rfloor is xx rounded down and umod1024u \bmod 1024 is the remainder of uu divided by 1024. A sequence of 16-bit units is well-formed UTF-16 exactly when every high surrogate (D800 to DBFF) is immediately followed by a low surrogate (DC00 to DFFF) and every low surrogate immediately follows a high one. UTF-32 uses one code unit equal to vv, and a 32-bit unit that is not a scalar value is ill-formed.

Four meanings of length

What a reader sees as one character can be several scalar values. Unicode calls such a user-perceived character a grapheme cluster, and Unicode Standard Annex #29 gives the rules that find cluster boundaries. The letter é can be the single scalar value U+00E9, or the letter e (U+0065) followed by U+0301, combining acute accent; the two look identical. A family emoji is the scalar values for man, woman and girl (U+1F468, U+1F469, U+1F467) joined by two U+200D, zero width joiner:

Text Scalar values UTF-8 bytes UTF-16 units Clusters
é as U+00E9 1 2 1 1
é as U+0065 U+0301 2 3 2 1
U+1F600 1 4 2 1
family emoji 5 18 8 1

The family emoji needs 4+3+4+3+4=184 + 3 + 4 + 3 + 4 = 18 UTF-8 bytes, because each joiner takes 3 bytes, and 2+1+2+1+2=82 + 1 + 2 + 1 + 2 = 8 UTF-16 units. A buffer is sized in bytes, while a text cursor moves by grapheme clusters.

Ill-formed input

A decoder must not turn an ill-formed sequence into a character.

Ill-formed is not the same as unwanted. A noncharacter such as U+FFFF (EF BF BF in UTF-8), or an unassigned scalar value, has a well-formed encoding, and whether to accept it is the application’s decision.

A decoder that does not reject substitutes U+FFFD, replacement character, for each ill-formed part, which loses information. Silently deleting the ill-formed bytes is worse: the pieces on either side join, and two fragments that a filter checked separately can form a forbidden string.

Byte order mark and normalization

The byte order mark (BOM) is U+FEFF placed at the start of a text to show its byte order: FE FF means big-endian UTF-16, FF FE little-endian. Plain UTF-16 without a BOM is read as big-endian unless a higher-level protocol says otherwise. In UTF-16BE and UTF-16LE, which name their order, a leading U+FEFF is an ordinary invisible character. UTF-8 has no byte order, but some tools start a UTF-8 file with EF BB BF, the UTF-8 form of U+FEFF. A parser that does not expect those 3 bytes fails or keeps them in the first field, and a signature over the bytes changes when a tool adds or removes them.

The two forms of é above, C3 A9 and 65 CC 81 in UTF-8, are canonically equivalent: Unicode says they mean the same text. Decoding keeps them different. Unicode normalization §15.19 is the separate step that converts text into one chosen form so that equivalent texts compare equal.

Limits and common mistakes

Decoding recovers scalar values and nothing more. It does not normalize, does not fold upper and lower case together, and does not detect look-alike characters such as the Latin letter a (U+0061) and the Cyrillic letter a (U+0430). Whether a name is acceptable is a further decision that the protocol must specify.

A check, a hash or a signature must state whether it covers the original bytes, the decoded scalar values or the normalized text. The two forms of é are equal as normalized text but differ as bytes and as scalar values.

Questions answered

Is Unicode a 16-bit encoding?

No. Unicode assigns numbers, called code points, to characters, and those numbers run from U+0000 to U+10FFFF, which needs 21 bits. Unicode defines three encoding forms with different code-unit widths: UTF-8 (Unicode Transformation Format, 8-bit) uses one to four 8-bit units per character, UTF-16 one or two 16-bit units, and UTF-32 one 32-bit unit. For example, U+1F600, grinning face, is the bytes F0 9F 98 80 in UTF-8 and the surrogate pair D83D DE00 in UTF-16. The belief dates from early Unicode, which had only 65,536 code points.

How do code point and scalar value differ?

Every Unicode scalar value is a code point, but the 2,048 surrogate code points U+D800 to U+DFFF are not scalar values. Surrogates exist only to build surrogate pairs in UTF-16 (Unicode Transformation Format, 16-bit); they never stand for characters, and a lone surrogate is ill-formed text. A noncharacter such as U+FFFF, or an unassigned scalar value, is a different case: it has a well-formed encoding, and only an application’s policy can reject it.

Why can length mean several different numbers for the same visible text?

Length can mean several numbers because it can count four different units: bytes, code units of an encoding, scalar values, and grapheme clusters, the characters a reader perceives. The letter é written as U+00E9 is 1 scalar value and 2 bytes in UTF-8 (Unicode Transformation Format, 8-bit); written as e followed by the combining accent U+0301, it looks the same but is 2 scalar values and 3 bytes. Both are 1 grapheme cluster. Python §12.23 counts code points and Java counts 16-bit code units, so an emoji that is one code point above U+FFFF has length 1 in Python and 2 in Java.

Does UTF-8 decoding normalize or authorize an identifier?

No. Decoding UTF-8 (Unicode Transformation Format, 8-bit) only checks that the bytes are well formed and recovers the scalar values they encode. It keeps the two equivalent spellings of é (U+00E9, and e followed by U+0301) different, does not fold case, and does not notice that the Cyrillic letter U+0430 looks like the Latin a. Normalization, case folding, look-alike checks and the decision whether the identifier is allowed are separate steps, which a protocol must specify, in a fixed order, before a name is used to grant anything.

May a receiver repair malformed input silently?

Only if the interface states that it repairs and how, and never by turning an ill-formed sequence into the character it seems to spell. Replacing each ill-formed part with U+FFFD, replacement character, is a recognized repair for text that is only displayed, but it changes the data. Deleting bad bytes is worse: the pieces on either side join into a string that no earlier check saw. For identifiers, keys, file names and authenticated data, rejecting malformed input is the safer contract.

Related entries: Endianness (§4.21), Unicode normalization (§15.19), Canonical JSON (§12.28), Authentication coverage (§5.41). Further reading: [192, 193, 194, 195, 511, 512].