prize18-2 Exercise 1: Encode One Character by Hand ==================================================== Steps for U+20AC (euro sign). It is in U+0800-U+FFFF, so it uses the 3-byte pattern 1110xxxx 10xxxxxx 10xxxxxx (16 free bits). 1. 16-bit binary of 0x20AC: 0010 000010 101100 2. Split 4 + 6 + 6 bits: 0010 | 000010 | 101100 3. Add the pattern bits: 11100010 | 10000010 | 10101100 4. In hex: E2 82 AC ex1.py: cp = 0x20AC print(f"{cp:016b}") b1 = 0b11100000 | (cp >> 12) b2 = 0b10000000 | ((cp >> 6) & 0b111111) b3 = 0b10000000 | (cp & 0b111111) mine = bytes([b1, b2, b3]) print(" ".join(f"{b:08b}" for b in mine), mine.hex(" ")) print(mine == "\u20ac".encode("utf-8")) Real output (Python 3.14): 0010000010101100 11100010 10000010 10101100 e2 82 ac True Note how the first byte starts with three 1s (a 3-byte character) and each later byte starts with 10 (continuation).