Field notes on text. Opinion first: most encoding bugs are not hard. They happen because someone said “character” when they meant one of three different things, and nobody noticed.
Three layers, three words
Unicode is a big catalogue. Every symbol gets a number, called a code point. Capital A is 65. The Devanagari letter न is another number. The smiley emoji is another. That is all Unicode does: it assigns numbers.
An encoding decides how those numbers become bytes on disk or on the wire. UTF-8, UTF-16 and UTF-32 are encodings of the same catalogue. Same code points, different bytes.
Then there is what a human sees as one unit on screen. Often that is one code point. Sometimes it is several glued together. Keep all three in your head: code points, bytes, and what the reader perceives.
What UTF-8 actually does
UTF-8 uses between one and four bytes per code point. Plain English letters, digits and common punctuation take one byte, identical to old ASCII. Accented Latin letters like é take two. Most Indian scripts, including Devanagari, Tamil, Telugu and Bengali, take three bytes per code point. Emoji take four. I checked these; do not rely on memory for this sort of thing.
Take the Hindi word नमस्ते. It has six code points. In UTF-8 it is 18 bytes. The English word “hello” is five code points and five bytes. If you size a buffer, a file field or a network packet by the English intuition, Hindi text will overflow it at three times the rate.
My opinion: UTF-8 should be your default everywhere. Files, databases, APIs, HTML. It won because ASCII stays unchanged, it has no byte-order problem, and it is what browsers and most of the web expect. Choose something else only with a written reason.
“Length” has no single answer
Ask for the length of a string and you get whatever your language decided to count. Python counts code points, so the smiley has length 1. JavaScript counts UTF-16 units, so the same smiley has length 2, because it sits outside the range two bytes can hold and gets split into a pair. Java strings behave like JavaScript here. Rust and Go give you the byte count by default.
Now the nasty case. The letter é can be one code point, or the plain letter e followed by a separate combining accent. They look identical. They are not equal when compared. In Python, the first has length 1, the second length 2, and an equality check says false. Normalisation turns one form into the other. If you compare usernames, search keys or file names, normalise first, typically to the composed form called NFC. Skip this and you get a user who cannot log in with a name that looks exactly right.
Indian scripts make the third layer obvious. A syllable the reader sees as one unit is often built from a consonant, a vowel sign and a joiner, each its own code point. Cutting a string at “ten characters” can slice a syllable in half and leave a broken glyph. If your app truncates names or previews, use a library that understands grapheme clusters instead of slicing by index.
Where data actually gets lost
- MySQL’s old utf8. Historically the
utf8character set in MySQL stored at most three bytes per character. Emoji need four. Useutf8mb4. Check your own version and defaults rather than assuming. - Column limits. Whether a
VARCHAR(50)means 50 characters or 50 bytes depends on the database. Read the documentation for yours. - Mismatched declarations. The file is UTF-8, the browser or reader assumes an older single-byte encoding, and you see garbage like é where é should be. The name for this is mojibake. The fix is to declare the encoding at every boundary, not to patch the output.
- CSV opened in Excel. Excel often guesses the encoding wrongly for a plain UTF-8 CSV with non-English text. Many teams add a byte-order mark to the file so Excel recognises it. Test it with your actual users’ Excel.
Habits worth keeping
Decode bytes to text once, at the edge where data enters. Work in text inside the program. Encode once, at the edge where it leaves. Do not scatter conversions through the middle.
Never try to “fix” garbled text by guessing; find where the wrong encoding was assumed. And never use byte length as a stand-in for the number of letters a person typed, in either direction. Validation rules like “name must be 3 to 50 characters” need a decision: code points, or grapheme clusters, and the same decision on the frontend and the backend.
One test I recommend adding to any project handling names: a Tamil name, a Hindi name, an emoji and an accented Latin name, each through the full path from form to database to PDF and back. It takes ten minutes and finds the problem before a customer does.
Leave a Reply