Mojibake

On this page 7

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 asRead asTypical result for “é”Recoverable
UTF-8Windows-1252éUsually yes
Windows-1252UTF-8� (U+FFFD)No, bytes discarded
UTF-8Shift_JISKanji-like noiseOften yes
UTF-16LEUTF-8 or ASCIILetters with null bytes betweenYes
UTF-8 twiceWindows-1252 twiceéYes, with two passes
Common encoding mismatches and their visible signatures

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.

BoundaryWhere the encoding is declaredTypical failure
HTTP and HTMLContent-Type charset, meta charsetHeader and document disagree
EmailMIME Content-Type, encoded wordsMissing charset on a header
DatabasesServer, connection and column charsetsClient connection set to Latin-1
CSV and plain textNowhere, or an optional BOMSpreadsheet guesses a legacy code page
Command lineLocale and console code pagePipe between tools with different defaults
File names in archivesArchive flag or noneZIP created on one OS, opened on another
Typical interoperability boundaries where mojibake appears

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 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.

ProblemWhat happenedTypical symptom
MojibakeCorrect bytes, wrong decoderPlausible but wrong characters
Lossy conversionCharacters missing from target encodingQuestion marks replace characters
Missing glyphsCorrect characters, font lacks themEmpty boxes, often called tofu
Endianness errorUTF-16 byte pairs swappedCharacters from unrelated scripts
Newline mismatchLine ending convention differsLines merged or extra symbols
How mojibake differs from other text problems

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.

LayerRecommended practiceReason
Web pages and APIsSend charset=utf-8 in Content-TypeRemoves browser guessing
DatabasesUse a full Unicode charset such as utf8mb4Older utf8 in MySQL stores only three bytes
Source codeOpen files with an explicit encodingPlatform defaults differ
CSV exportsDocument the encoding, add a BOM if consumers need itSpreadsheets otherwise guess
TestsInclude non-ASCII and emoji sample dataExposes mismatches before release
Prevention measures by layer

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.