Mojibake is the garbled, unreadable text that appears when bytes written in one character encoding are decoded with another, so that “café” turns into “café” or Japanese text turns into rows of accented Latin letters. The word comes from Japanese and roughly means “character transformation”. In software interoperability, mojibake is one of the most visible symptoms of a broken data exchange: the bytes themselves are usually intact, but the metadata about how to read them was lost, wrong or ignored.
Mojibake matters because it is often silent and cumulative. A program rarely reports an error; it simply displays or stores the wrong characters, and every further save, import or conversion can make the damage harder or impossible to reverse.
How mojibake arises from an encoding mismatch
Mojibake arises because text on disk or on the wire is only a sequence of bytes, and the same bytes map to different characters under different encodings. A writer encodes characters into bytes with encoding A; a reader decodes those bytes with encoding B. When A and B disagree on any byte value that occurs in the data, the reader produces substitute characters instead of the originals.
The classic case is UTF-8 read as Windows-1252 (or ISO-8859-1). In UTF-8, “é” is the two bytes C3 A9. A reader that assumes Windows-1252 treats each byte as its own character and shows “Ô followed by “©”. Because UTF-8 uses one to four bytes per character, each non-ASCII character becomes two to four wrong characters, which is why mojibake text is visibly longer than the original.
The reverse mistake looks different. Bytes written in Windows-1252 and read as UTF-8 usually contain invalid UTF-8 sequences, so a strict decoder raises an error and a lenient one inserts the replacement character U+FFFD, shown as a question mark in a diamond. The table below summarizes the most common mismatch patterns and their recognizable signatures.
| Written as | Read as | Typical result for “é” | Recoverable |
|---|---|---|---|
| UTF-8 | Windows-1252 | é | Usually yes |
| Windows-1252 | UTF-8 | � (U+FFFD) | No, bytes discarded |
| UTF-8 | Shift_JIS | Kanji-like noise | Often yes |
| UTF-16LE | UTF-8 or ASCII | Letters with null bytes between | Yes |
| UTF-8 twice | Windows-1252 twice | é | Yes, with two passes |
Where the encoding information gets lost
Mojibake almost always originates at a boundary between systems, where text crosses from one program, protocol or storage layer to another. Each boundary has its own way of declaring the encoding, and each one can drop or misstate that declaration.
| Boundary | Where the encoding is declared | Typical failure |
|---|---|---|
| HTTP and HTML | Content-Type charset, meta charset | Header and document disagree |
| MIME Content-Type, encoded words | Missing charset on a header | |
| Databases | Server, connection and column charsets | Client connection set to Latin-1 |
| CSV and plain text | Nowhere, or an optional BOM | Spreadsheet guesses a legacy code page |
| Command line | Locale and console code page | Pipe between tools with different defaults |
| File names in archives | Archive flag or none | ZIP created on one OS, opened on another |
Plain text files are the weakest link because they carry no mandatory encoding declaration. Software must then rely on defaults or heuristics. Defaults differ by platform and version: older Windows applications assumed the system ANSI code page, while modern Linux, macOS and web tooling assume UTF-8. The byte order mark can mark a file as UTF-8 or UTF-16, but many tools ignore it or even display it as stray characters.
Mojibake compared with related text corruption
Mojibake is a specific decoding failure, and distinguishing it from neighboring problems speeds up diagnosis. The key test is whether the original bytes are still present and merely misinterpreted, or whether information has actually been destroyed.
| Problem | What happened | Typical symptom |
|---|---|---|
| Mojibake | Correct bytes, wrong decoder | Plausible but wrong characters |
| Lossy conversion | Characters missing from target encoding | Question marks replace characters |
| Missing glyphs | Correct characters, font lacks them | Empty boxes, often called tofu |
| Endianness error | UTF-16 byte pairs swapped | Characters from unrelated scripts |
| Newline mismatch | Line ending convention differs | Lines merged or extra symbols |
The distinction has practical consequences. Missing glyphs are fixed by installing a font, and the stored data is fine. Lossy conversion to ASCII or a legacy code page has already replaced the characters, so no decoder can restore them. True mojibake, by contrast, is frequently reversible as long as nothing has re-saved the wrong characters through a lossy step.
Detecting and repairing mojibake
Repair works by reversing the wrong decoding: re-encode the garbled string with the encoding that was wrongly assumed, then decode the resulting bytes with the correct encoding. For the UTF-8 read as Windows-1252 case, encoding “café” back to Windows-1252 yields 63 61 66 C3 A9, which decodes correctly as UTF-8 to “café”. Libraries such as the Python package ftfy automate this round-trip reversal with heuristics for chains of repeated errors.
Detection relies on statistical signatures. Sequences such as “Ô, “— and “” followed by punctuation are strong indicators of UTF-8 decoded as Windows-1252. Encoding detectors such as chardet or ICU estimate the encoding from byte frequencies, but they are probabilistic: short strings and texts with few non-ASCII characters are frequently misclassified.
- Inspect the raw bytes with a hex viewer before converting anything.
- Work on a copy, because a failed repair attempt can add another layer of damage.
- Check every layer: file, connection, column and display settings.
- Validate the result against text with known correct accents or scripts.
Preventing mojibake in software systems
The durable prevention strategy is to use UTF-8 end to end and to declare the encoding explicitly at every boundary. Unicode-based UTF-8 is the dominant encoding on the web and in modern operating systems, so standardizing on it removes most conversion points. Code should decode bytes once at input, work internally with Unicode strings, and encode once at output, a pattern often called the Unicode sandwich.
| Layer | Recommended practice | Reason |
|---|---|---|
| Web pages and APIs | Send charset=utf-8 in Content-Type | Removes browser guessing |
| Databases | Use a full Unicode charset such as utf8mb4 | Older utf8 in MySQL stores only three bytes |
| Source code | Open files with an explicit encoding | Platform defaults differ |
| CSV exports | Document the encoding, add a BOM if consumers need it | Spreadsheets otherwise guess |
| Tests | Include non-ASCII and emoji sample data | Exposes mismatches before release |
A common mistake is to fix the display instead of the data, for example by changing a viewer setting until the text looks right while the stored bytes stay wrong. Another is double encoding, where already garbled text is encoded to UTF-8 again, which multiplies the damage with each pass.
Stripped to the essentials
Computers store letters as numbers, and an encoding is the codebook that says which number means which letter. Mojibake is what you see when the reader uses the wrong codebook. The numbers are still there, but they are translated into the wrong symbols, so a name like “José” appears as “José”.
It is a bit like reading a message written in one shorthand system with the key for another. The message is usually not lost, only misread, so it can often be fixed if nobody has rewritten it in the meantime.
Where it is applied
A retailer moves its customer database from an older server to a new cloud platform. After the import, about 4 percent of customer names in the new system show sequences such as “Müller” and “Björk”. The team traces the problem to the export script, which wrote UTF-8 bytes while the import tool was configured to read Windows-1252.
Instead of correcting names by hand, a developer runs a script on a copy of the table that re-encodes each affected field as Windows-1252 and decodes it as UTF-8, checking each result for valid characters. The import job is then rerun with an explicit UTF-8 setting and the database column switched to utf8mb4. All affected records are restored automatically, and a test with accented and emoji names is added to the migration checklist.