Search HoldMyTwist

Browse all posts

Episode 20

The Three-byte Star

Duck tries to get a sign in an envelope. Robot explains why a character isn't always exactly one UTF-8 byte.

Episodes

Computing Basics

    Duck tries to get a sign in an envelope.
    Robot displays selected utf8-rule. In monitor A shows one enclosing byte container which has 8 bit compartments (zero one zero zero zero zero zero one).
    Duck approved the 1 byte slot.
    The specific code points we're talking about are A U+0041, precomposed é U+00E9, and ★ U+2605.
    Robot finds 2 hex-byte slots C3 A9 under precomposed é U+00E9. Duck tries to squeeze them in to 1 slot but fails.
    Robot points to the single character which is above two slots.
    Robot finds three separate slots E2 98 85 below ★ U+2605.
    Duck pushes a small three-box trolley under the star row in the monitor.
    Robot puts A-row with 1 byte next to accented-e row with 2 bytes and star row with 3 bytes.
    Duck holds Aé★ on empty 6-cell byte strip. Robot shows only three rows of components without total.
    Duck inserts it into envelope, with six slots.
    Robot compares 2 messages both UTF-8 encoded (no byte order mark).
    Both messages are made of six byte cells.
    Duck sets up new message one A and two stars above known encoding rows. Robot conceals new total.
    Duck does the group counting.
    Duck stuffs the 7-bytes into the envelope and closes it.
    Duck preserves the final sign with correct envelope.
    Duck puts the old three slot envelope away. He's holding the new seven slot envelope.

    Episode transcript

    Three symbols

    Duck tries to get a sign in an envelope.

    Diagram values: Aé★; ENVELOPE: 3 BYTE SLOTS

    Duck: My sign has three characters on it. Three bytes should be enough.

    Robot displays selected utf8-rule. In monitor A shows one enclosing byte container which has 8 bit compartments (zero one zero zero zero zero zero one).

    Diagram values: UTF-8; NO BOM; A: 01000001; 1 BYTE = 8 BITS

    Robot: We will use UTF-8 to encode these symbols to bytes.

    Duck approved the 1 byte slot.

    Diagram values: A U+0041; 41; 1 BYTE

    Duck: Smart customer.

    The specific code points we're talking about are A U+0041, precomposed é U+00E9, and ★ U+2605.

    Diagram values: é U+00E9; ★ U+2605

    Robot: Will it be one byte for each of the other symbols?

    Pack the bytes

    Robot finds 2 hex-byte slots C3 A9 under precomposed é U+00E9. Duck tries to squeeze them in to 1 slot but fails.

    Diagram values: é U+00E9; C3 A9; 2 BYTES

    Robot: This accented e requires 2 bytes (C3 and A9).

    Robot points to the single character which is above two slots.

    Duck: I was hopeful for my envelope.

    Robot finds three separate slots E2 98 85 below ★ U+2605.

    Diagram values: ★ U+2605; E2 98 85; 3 BYTES

    Robot: The star consists of three bytes, E2, 98 and 85.

    Duck pushes a small three-box trolley under the star row in the monitor.

    Duck: That star carried its own luggage

    Robot puts A-row with 1 byte next to accented-e row with 2 bytes and star row with 3 bytes.

    Diagram values: A:1; é:2; ★:3

    Robot: UTF-8 supports different numbers of bytes for different code points.

    Count a message

    Duck holds Aé★ on empty 6-cell byte strip. Robot shows only three rows of components without total.

    Diagram values: Aé★

    Duck: So how big in bytes does it need to be for the entire sign?

    Duck inserts it into envelope, with six slots.

    Diagram values: 41 C3 A9 E2 98 85; 6 BYTES

    Robot: That's 6 bytes of UTF-8.

    Robot compares 2 messages both UTF-8 encoded (no byte order mark).

    Diagram values: ★★; E2 98 85 E2 98 85; 6 BYTES

    Duck: 6 bytes for 2 stars.

    Both messages are made of six byte cells.

    Diagram values: Aé★:6 BYTES; ★★:6 BYTES

    Robot: Counting characters is not enough to count bytes.

    One extra letter

    Duck sets up new message one A and two stars above known encoding rows. Robot conceals new total.

    Diagram values: A★★

    Duck: In UTF-8 exactly how many bytes?

    Duck does the group counting.

    Diagram values: 1 + 3 + 3

    Robot: The stars need three bytes each.

    Duck stuffs the 7-bytes into the envelope and closes it.

    Diagram values: 41 E2 98 85 E2 98 85; 7 BYTES

    Duck: 7 bytes.

    Duck preserves the final sign with correct envelope.

    Diagram values: é U+00E9; UTF-8 NO BOM

    Robot: These bytes are fixed by that precise set of symbols and encoding.

    Duck puts the old three slot envelope away. He's holding the new seven slot envelope.

    Duck: Next time I'll make sure I count the number of bytes before I pick an envelope.

    The lesson

    UTF-8 is a variable-length encoding of Unicode code points. The specific code points we're talking about are A U+0041, precomposed é U+00E9, and ★ U+2605. The specific UTF-8 byte values in hexadecimal for these are 41, C3 A9 and E2 98 85 respectively. When byte order mark is absent, the UTF-8 for Aé★ is 6 bytes long, just like the UTF-8 for ★★. Note that we're making a distinction here between code points (the abstract thing) and the bytes (the representation in memory). Sometimes characters that can be seen by the reader can be represented by more than one code point, so that is not necessarily a reliable way to count visible characters.

    Try it

    Diagram values: UTF-8, NO BOM; ééA; é = U+00E9

    What is the size of string encoding ééA in UTF-8 (no BOM) format? Both accented e's are the same precomposed U+00E9 character from the story. In UTF-8 exactly how many bytes?

    Answer

    Diagram values: 2 + 2 + 1 = 5 BYTES; C3 A9 C3 A9 41

    Each precomposed accent e costs 2 bytes. A costs 1 byte. 2+2+1 = 5 bytes. hex C3 A9 C3 A9 41

    Sources

    UTF-8 byte lengths (opens in a new tab)