A character is not a byte
Three layers matter: code points, the numbers Unicode assigns, written as U plus four or more hex digits; code units, the storage element of an encoding, one byte in UTF-8 and two in UTF-16; and graphemes, what a person sees as one character, possibly several code points combined. Latin A is U+0041 and one UTF-8 byte; the Chinese character zhong is U+4E2D and three bytes; the rocket emoji is U+1F680, four bytes in UTF-8 and two code units in UTF-16.
Steps
- Paste any text, including Chinese, emoji and combining marks.
- Analyse to list code point, UTF-8 bytes, UTF-16 units, category and display width per character.
- Load the sample covering A, a Chinese character and an emoji.
- Copy the table or clear.
- For range questions, compare the code point against block boundaries.
Width and alignment in practice
In terminals and monospace fonts, Chinese and emoji occupy two columns while Latin occupies one — the East Asian width property. Counting characters mis-measures width and breaks table alignment. Truncation is worse: cutting by code unit splits emoji into unrenderable halves, cutting by code point is safer, and only grapheme truncation matches what a reader calls one character. Combining marks add a modifier code point, so length is two while display is one.
Normalisation and equivalence
One visible letter can have several representations — a precomposed code point or a base plus combining mark — identical on screen and different in bytes, so string comparison disagrees. Normalisation unifies equivalent sequences, so normalise before comparing. Full-width versus half-width Latin, space variants and zero-width characters all cause text that looks the same but cannot be found, which is worth checking when hunting duplicates.
Common mistakes
Treating length as a character count, when JavaScript reports code units and counts an emoji twice. Truncating by bytes, yielding mojibake. Ignoring zero-width joiners copied from the web, invisible but breaking comparison. Assuming an emoji is one code point, when skin tone and gender variants combine several. And conflating blocks: rare Chinese characters live in extension planes and render as boxes without the right font.
When per-character inspection helps:It pinpoints why text cannot be found, why two identical-looking names mismatch, why exported columns misalign and why truncation produces replacement boxes. For input validation, decide whether limits count code points or graphemes, since users feel the difference. For localisation, confirm font coverage. Elsewhere, a string library beats hand-written inspection.