> codebench_TOOL: HomeLOCAL-ONLY⌘K Switch

UTF-8 Bytes vs. Characters: Why Café Can Take 5 or 6 Bytes

Paste two versions of Café into a character counter. See what changes in UTF-8, what stays the same on screen, and which length to use for an input limit.

·2 min readAbout the maintainer
The word Café on a type baseline, with two possible UTF-8 lengths: 5 bytes and 6 bytes.

Suppose a tiny label printer allows five UTF-8 bytes for a shop name. You type Café and it fits. A colleague pastes what looks like the same name from a catalog, and the printer rejects it. Neither of you added a space. The difference is in the last letter: did the catalog store an accented é, or an e followed by an accent?

Paste the names one at a time

Open the Character Counter. Copy just the word from this first block, without a trailing line break:

Café

The counter shows Total: 4 and UTF-8: 5 B. The C, a, and f each take one byte. The last letter is a single Unicode code point, U+00E9, which needs two bytes in UTF-8.

Replace it with the word from this second block. Copy it rather than retyping it; your keyboard might produce the first version again.

Café

Now the result is Total: 5 and UTF-8: 6 B. In the frequency table, look for U+0065 (a plain e) and U+0301 (a combining acute accent). The accent has a row of its own even though it appears on the letter before it. If either total is one higher than expected, look for U+000A: that's a line feed from the paste.

The last letter alone: U+00E9 encodes as C3 A9, while U+0065 followed by U+0301 encodes as 65 CC 81.
The first three bytes of both names are identical. Only the highlighted ending changes. To inspect the bytes yourself, load each word in the Hex Viewer's Text mode.

Which count does the limit mean?

For the printer, the byte budget is the contract. For a text field promising “four characters,” the person typing is more likely to expect four visible units. An extended grapheme cluster approximates that unit: the e and its combining mark form one cluster. Both versions of the name contain four clusters. The counter's Total counts code points instead; it isn't a grapheme count.

JavaScript adds another useful but different measure. A string's .length counts UTF-16 code units. Try a single 🍋: it is one code point and one grapheme cluster, but its JavaScript length is two and it takes four UTF-8 bytes. That is why a check like name.length <= 5 is not a five-byte check.

const name = "Café";
const segments = new Intl.Segmenter("en", { granularity: "grapheme" });

[...segments.segment(name)].length;    // 4
[...name].length;                      // 5
name.length;                           // 5
new TextEncoder().encode(name).length; // 6

Make an explicit choice about normalization

If your label format accepts normalized text, name.normalize("NFC") turns the decomposed ending in this example into U+00E9. Measure the normalized string, then send that same string to the printer. Measuring one version and transmitting the other would bring the bug right back.

Unicode normalization is not a way to squeeze arbitrary words into five bytes. It preserves canonical equivalence; not every combination has a single precomposed character. If the job is to reproduce the catalog file byte for byte, keep the original and inspect it in the Hex Viewer instead. For a length error, the most useful question to ask first is: a limit on what, measured at which step?

#unicode#utf-8#javascript#text
> codebench $Ctrl+Enter Run