UTF-8 and Unicode Explained: Why Accented Text Breaks
7 min read · Published 1 November 2026 · Updated 2 November 2026
If you have ever seen "á" where an "á" should be, or found that two identical-looking Vietnamese words are not equal in code, you have met the details of Unicode and UTF-8. Handling text correctly means understanding a few ideas that are easy to mix up: characters, code points and bytes. This guide explains them with examples, so you can store, compare and search multilingual text without surprises.
In this guide
Unicode and UTF-8 are different things
Unicode is a catalogue that gives every character a number, called a code point, written like U+0041 for "A" or U+1EBF for "ế". It says nothing about bytes. An encoding is the rule for storing those numbers as bytes. UTF-8, UTF-16 and UTF-32 are three encodings of the same Unicode characters. UTF-8 is the dominant encoding on the web: it uses one to four bytes per character, and the first 128 characters (plain ASCII) use a single byte, which keeps it compatible with older text.
Why Vietnamese text takes more bytes
Letters without accents, like "a", take one byte in UTF-8. Letters such as "ế" are outside ASCII and take three bytes (E1 BA BF). A line of Vietnamese text is therefore longer in bytes than the same number of English letters. This matters when a limit is counted in bytes: some database column definitions, network protocols and file formats count bytes, not characters.
One letter, two representations
Many accented letters can be written in two valid ways. "ế" can be a single code point (U+1EBF, called the precomposed form), or three code points in a row: "e" plus a combining circumflex (U+0302) plus a combining acute accent (U+0301). The two look identical on screen but are different sequences, so a plain string comparison says they are not equal, and a search for one will not find the other. Text from different keyboards, operating systems and copy and paste sources can use either form.
JavaScript
const a = "\u1EBF"; // "ế" as one code point
const b = "e\u0302\u0301"; // "ế" as e + two combining marks
a === b; // false
a.normalize("NFC") === b.normalize("NFC"); // trueNormalise before comparing and searching
Unicode defines normalisation forms. NFC composes characters into their shortest form, and NFD decomposes them. Pick one (NFC is the usual choice for storage and display) and normalise text when it enters your system and before you compare, deduplicate or index it. To search without regard to accents, a separate step removes the combining marks after decomposing to NFD.
What "length" means
The same string can have several lengths. In JavaScript, "😀".length is 2, because strings are counted in UTF-16 units and this emoji needs two. The decomposed "ế" above has length 3, the composed one has length 1, but both are one visible character, called a grapheme cluster. When you enforce limits such as "at most 50 characters", decide what you count: bytes, code points or what the user sees, and use a library that handles grapheme clusters if it matters.
How garbled text (mojibake) happens
Garbled text happens when bytes written in one encoding are read as another. The word "á" is stored in UTF-8 as the two bytes C3 A1. If a program reads those bytes as Latin-1, it shows "á". Fix it at the source: make every step agree on UTF-8. That means the file or database, the connection, the HTTP header (Content-Type with charset=utf-8) and the HTML declaration (<meta charset="utf-8">).
Databases: use utf8mb4
In MySQL, the character set called "utf8" is a limited three-byte variant that cannot store characters outside the basic plane, such as emoji. Use utf8mb4, which is real UTF-8. In PostgreSQL, a UTF8 database encoding handles all of Unicode. Also remember that a VARCHAR(n) limit counts characters in most databases, but the storage and index size limits are in bytes.
Messages and other limits
Old text-message standards use a restricted alphabet and allow 160 characters per message. A single character outside it, which includes most Vietnamese letters with accents, switches the message to a Unicode encoding with a limit of 70 characters per message. A text that looks short can therefore be split into several messages, and cost more.
Practical rules
- Use UTF-8 everywhere: files, databases, APIs, HTML and HTTP headers.
- Normalise to NFC when text enters your system.
- Test with real text in the languages your users use, including accents, long words and emoji, not only English or lorem ipsum.
- Know whether a limit counts bytes or characters.
- Avoid cutting a string in the middle of a character. Use functions that respect code points or grapheme clusters.
Frequently asked questions
- What is a BOM?
- A byte order mark is a special character (U+FEFF) at the start of a file. In UTF-8 it appears as the bytes EF BB BF and is not needed to determine byte order, but some programs, especially spreadsheets, use it to detect that a file is UTF-8.
- Should I use UTF-8 or UTF-16?
- For files, network data and storage, UTF-8 is the usual choice. UTF-16 is used inside some platforms, such as JavaScript strings and Windows APIs. What matters is declaring the encoding correctly where text crosses a boundary.
- Why do two identical-looking strings compare as different?
- They probably use different Unicode forms, for example a precomposed letter versus a base letter with combining marks. Normalise both to the same form before comparing.
- How do I fix text that is already garbled?
- Find which encoding was used to read it, and re-read the original bytes as UTF-8. Text that was saved after being misread may be damaged and cannot always be repaired, so keep a copy of the source.
Sources and further reading
Try it yourself: Dummy Text Generator runs in your browser, free and with no sign-up.