TechCompare LogoTechCompare

How to fix mojibake: when UTF-8 text displays as gibberish and how to recover it

Mojibake is always caused by a mismatch between the encoding used to write bytes and the encoding used to read them. Fix it by identifying both encodings and reinterpreting correctly. Prevent it by using UTF-8 everywhere and declaring it explicitly.

Mojibake (Japanese for 'character transformation') is the garbled text you see when bytes written in one encoding are read as another. The classic example: UTF-8 bytes interpreted as Latin-1 produce strings like é instead of é. The data isn't corrupt. It's being misinterpreted. Fixing it means knowing what encoding it was written in and what encoding it's being read as.

By TechCompare · Updated

Encoding focus
Fixing mojibake
mojibake
Category
Pitfalls
Common encoding problems and fixes

How this is calculated

Common mojibake scenarios: a MySQL database column was created as latin1 but the application writes UTF-8 bytes into it, a CSV file exported from Excel doesn't include a BOM and is opened as ASCII, or an API response declares charset=ISO-8859-1 but actually returns UTF-8. Recovery depends on whether the bytes were transcoded or just mislabeled. If they were only mislabeled, reinterpret with the correct encoding. If they were double-encoded (UTF-8 bytes treated as Latin-1, then encoded to UTF-8 again), you need to reverse the double-encoding step by step. Prevention is simpler than recovery: always declare UTF-8 explicitly in HTTP headers, HTML meta tags, database schemas, and file formats.

Verdict

Three mislabeling scenarios cause most of the gibberish. A latin1 MySQL column receiving UTF-8 bytes, an Excel CSV exported without a BOM, and an API header lying about its charset. If the bytes were only mislabeled, reinterpret with the right encoding. If they were double-encoded, you have to reverse each layer in order. Prevention wins: declare UTF-8 in HTTP headers, meta tags, and schema definitions.

More Encoding scenarios

Base64 vs Hex
Base64 and hexadecimal both encode binary data as text, but they serve different purposes.
View details ➜
UTF-8 vs ASCII
ASCII maps 128 English characters to 7-bit values.
View details ➜
URL encoding guide
Percent-encoding (also called URL encoding) replaces characters that aren't safe in a URL with a percent sign followed by two hex digits.
View details ➜

Frequently asked questions

What is mojibake and what causes it?
Mojibake is garbled output like é where é belonged. It happens when bytes written in one encoding get decoded as another - most often UTF-8 read as Latin-1. The underlying bytes are intact. The reader is just using the wrong translation table, which is why mojibake is usually fixable once you identify both ends of the mismatch.
Why does my database show é instead of é?
The classic cause is a MySQL column declared latin1 receiving UTF-8 bytes from the application. The client wrote UTF-8, the table claims latin1, and reading back translates each UTF-8 byte into a separate Latin-1 character. Fix the column to utf8mb4, then re-import or repair the existing rows that were double-baked.
How do I fix text that was double-encoded to UTF-8?
Reverse the layers one at a time. Double-encoding usually means UTF-8 bytes were misread as Latin-1 and then saved as UTF-8 again, so you decode the string as UTF-8, reinterpret the result as Latin-1 bytes, and decode those as UTF-8. Prevention beats repair: declare UTF-8 explicitly in HTTP headers, HTML meta tags, and database schemas.