UTF-8
UTF-8 is a variable-length, ASCII-compatible encoding of Unicode code points in 1 to 4 bytes, where the leading bits of the first byte give the length and continuation bytes start with 10.
Know these firstPrefix-Free Code
Formula#
| Bytes | Code point range | Payload bits | UTF-8 code |
|---|---|---|---|
| 1 | (0–7F) | 7 | 0xxxxxxx |
| 2 | (80–7FF) | 11 | 110xxxxx 10xxxxxx |
| 3 | (800–FFFF) | 16 | 1110xxxx 10xxxxxx 10xxxxxx |
| 4 | (10000–) | 21 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
Procedure: write the code point in binary, pick the row by its range, pad with leading zeroes to the payload size, fill the x positions from left to right (the lowest bits go into the last byte), convert to hex if asked.
Properties#
- ASCII compatible: characters have the identical single byte, so any such string is valid UTF-8.
- ISO-8859-1 compatible in a simple way: code
1xyyyyyybecomes1100001x 10yyyyyy; ISO-8859-1252(ü) is UTF-8195 188(C3 BC), URL-escaped%C3%BC(DBIS Exam WS1516 (not published), Task 3.4). - Self-synchronizing: continuation bytes start with
10; decoding can start anywhere by skipping to the next byte not starting with10. The number of leading 1s of a first byte equals the number of bytes. All bytes of a multi-byte character are . - Overlong encodings are invalid: a code point that fits a shorter form must use it. E.g.
1100000x 10xxxxxxis invalid;C1 BF(code point 127) is invalid,CF BF(code point 1023) is valid;1100 0001 1000 0000(code point 64) is invalid (DBIS Exam WS2122 (not published) Task 2.4, DBIS Exam WS2324-2 (not published) Task 4.2). Not every byte sequence is valid UTF-8. - Valid 2-byte sequences: first byte
C2–DF(C0,C1would be overlong), 30 values, second byte 64 values: (DBIS Exam WS1516 (not published), Task 3.3). - Valid 2-byte sequence check: first byte
110xxxxxwith valueC2, second byte10xxxxxx. - The code is prefix-free (Prefix-Free Code) at the byte level.
Example#
- U+03B1 : 11 bits
01110 110001, template110xxxxx 10xxxxxxgives11001110 10110001CE B1. :11001110 10010001; they differ in one bit (bit 5 of the second byte), so lowercasing Greek letters means setting it (DBIS Exam WS1819 (not published), Task 2.3–2.4). - Euro
0010 0000 1010 1100: 3 bytes,1110 0010 10 000010 10 101100E2 82 AC(DBIS Exam WS1617 (not published), Task 3.3). - U+1F60E (sunglasses emoji):
1 1111 0110 0000 1110, 17 bits, 4 bytes, padded to 21:000 011111 011000 001110, giving11110000 10011111 10011000 10001110F0 9F 98 8E(DBIS Exam WS1920 (not published), Task 2.3). - U+08F0
0000 1000 1111 00001110 0000 1010 0011 1011 0000(DBIS Exam WS2324-2 (not published), Task 4.2).
Sources#
- Slides 10, p. 27, Slides 10, p. 28, Slides 10, p. 29
- DBIS_VL10 > UTF-32, UTF-16, UTF-8, DBIS_VL10 > UTF-8 in detail, DBIS_VL10 > Properties of UTF-8; bytes vs. strings in DBIS_VL09 > Bytes vs. strings
- Past exams: DBIS Exam WS1314 (not published) Task 3, DBIS Exam WS1516 (not published) Task 3, DBIS Exam WS1617 (not published) Task 3, DBIS Exam WS1718 (not published) Task 3.3, DBIS Exam WS1819 (not published) Task 2, DBIS Exam WS1920 (not published) Task 2, DBIS Exam WS2122 (not published) Task 2.4, DBIS Exam WS2324-2 (not published) Task 4.2