December 5, 2026

UTF-8 and Mojibake: Why “café” Turns Into “café”

Metal type blocks with accented letters and stray symbols on a wooden tray

The word “café” takes five bytes in UTF-8, one more than it has letters. Read those bytes back as Windows-1252, the old Western European code page, and the screen shows “café.” Nothing in the file was damaged. Every byte is exactly where it was written; the program reading it simply guessed the wrong decoder. That garbled text has a name, mojibake, and almost every case of it comes down to the same mismatch between how bytes were written and how they were read. The examples below were run in Python 3.13 and on the Linux command line, and every byte shown is the real output.

Video: Nic Barker on YouTube

Characters, code points and bytes

Three layers that get confused

A character is the abstract thing, the letter é. Unicode assigns each character a number called a code point; é is U+00E9. An encoding is the rule that turns that number into bytes on disk or on the wire. UTF-8 is one such rule, UTF-16 another. Mojibake happens at that last step, when bytes written under one rule are read under another.

How UTF-8 packs a number into bytes

RFC 3629, the November 2003 standard that defines UTF-8, lays out four patterns. Code points up to U+007F fit in a single byte starting with a 0 bit. Larger ones take two, three or four bytes: the first byte begins with 110, 1110 or 11110 to announce the length, and every continuation byte begins with 10. The RFC summarizes the range as characters “encoded using sequences of 1 to 4 octets,” and it limits that range to U+10FFFF.

Built to be found mid-stream

The bit patterns do a second job. Because every continuation byte starts with 10 and no leading byte does, a program dropped into the middle of a UTF-8 stream can skip forward to the next byte that doesn’t start with 10 and know it has reached the start of a character. A corrupted byte damages one character, not everything after it.

The ASCII guarantee

The design choice that made UTF-8 win is in the RFC’s first lines: UTF-8 “has the characteristic of preserving the full US-ASCII range.” A plain English file saved in ASCII is already valid UTF-8, byte for byte. That backward compatibility is also why mojibake hides so well. A file can be mislabeled for years without anyone noticing, until the first accented name, curly quote or emoji shows up.

White printing paper with numbers

The bytes, measured

What real text costs

Encoding a handful of strings in Python with s.encode('utf-8') gives these byte sequences:

Text UTF-8 bytes Byte count
café 63 61 66 c3 a9 5
naïve 6e 61 c3 af 76 65 6
em dash e2 80 94 3
€ e2 82 ac 3
日本 e6 97 a5 e6 9c ac 6
😀 f0 9f 98 80 4

The ASCII letters stay one byte each, exactly as the RFC promises. The é becomes c3 a9, a two-byte sequence beginning with 110 and 10 bit patterns. The em dash and euro sign need three bytes, each Japanese character three, and the emoji four.

Length depends on who’s counting

Python reports len('😀') as 1, because a Python string counts code points. The same emoji takes 4 bytes in UTF-8 and 4 in UTF-16, where it needs two 16-bit units. A database column limited to “10 characters” can mean three very different limits depending on whether it counts bytes, UTF-16 units or code points.

How mojibake happens

The classic pattern

Decoding the UTF-8 bytes of “café” as Windows-1252 maps each byte to its own character: c3 becomes à and a9 becomes ©. Running the same test on the other strings produced this:

Original Read as Windows-1252
café café
naïve naïve
em dash —
€ €
日本 日本
😀 😀

Anyone who has seen “’” where an apostrophe belongs has met the curly-quote version of this. The leading à or â is the giveaway: it is how the first byte of a two- or three-byte UTF-8 sequence looks in Windows-1252.

The quote that breaks differently

The curly quotes in “quoted” told a stranger story. The opening quote turned into ““,” but the closing one came out as “— followed by a replacement character, because its last byte, 9d, has no assigned character in Windows-1252. One mismatched decode can produce both readable gibberish and outright gaps in the same line.

Why latin1 and Windows-1252 blur together

The two are different code pages, but on the web they’re treated as one. The WHATWG Encoding Standard that browsers follow lists “iso-8859-1,” “latin1,” “ascii” and “us-ascii” all as labels for windows-1252. A page declaring itself Latin-1 is decoded as Windows-1252 regardless.

A stack of playing cards sitting on top of a wooden table

Double encoding and the wrong repair

Mojibake on top of mojibake

The damage compounds. Taking the already garbled “café,” encoding it as UTF-8 again and decoding it as Windows-1252 again produced “café.” Each round trip through the wrong decoder adds another layer of junk characters. Text that has been through a few badly configured systems, an export, an import, a copy into a CMS, can pick up two or three layers.

Reversing it

Because no bytes were lost in the first step, the first step can be undone. In the test, 'café'.encode('cp1252').decode('utf-8') returned “café” cleanly. The fix is the mistake run backward: turn the wrong characters back into the bytes they came from, then decode those bytes correctly.

When the repair fails

The reverse trick only works if every byte survived. The curly-quote example above shows why it can’t always: once the 9d byte was replaced by a replacement character, the original information was gone. A clean repair also requires knowing which wrong decoder was used. Guessing Windows-1252 when the culprit was something else can make the text worse.

The byte order mark

Three invisible bytes

Some editors write a byte order mark at the start of a UTF-8 file. RFC 3629 notes that in UTF-8 “the BOM will always appear as the octet sequence EF BB BF.” Python’s 'hi'.encode('utf-8-sig') produced exactly that: ef bb bf followed by 68 69.

Why it causes trouble

UTF-8 has no byte order to mark, so the BOM serves only as a signature. The Unicode Consortium’s FAQ says an initial BOM “is only used as a signature,” and warns that some recipients don’t expect one. It interferes with any format that “expects specific ASCII characters at the beginning.” A script beginning with #!/bin/sh or a CSV whose first column header is read as “name” instead of “name” are the usual casualties.

Python’s two codecs

Python’s codecs documentation handles this with a separate codec. Plain utf-8 keeps a leading BOM as a character. utf-8-sig adds one when writing and skips one when reading. The same page adds that “In UTF-8, the use of the BOM is discouraged and should generally be avoided,” so utf-8-sig is most useful for reading files from tools that insist on writing one.

Closeup photo of black and white keyboard keys

Finding and fixing encoding problems

Decode errors are a feature

The strict default is the most useful behavior. In the test, reading the Latin-1 bytes of “café” (63 61 66 e9) as UTF-8 raised UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 3. An error at the boundary points straight at the problem. Python’s documentation describes the alternative, errors='replace', as substituting “U+FFFD, the official REPLACEMENT CHARACTER,” which is how the test produced “caf�.” That keeps a program running but quietly destroys the original byte.

Checking a file on Linux

The file command guesses an encoding from the contents. Run on two test files, one written as UTF-8 and one as Latin-1, it reported “Unicode text, UTF-8 text” for the first and “ISO-8859 text” for the second. Asking iconv -f utf-8 -t ascii to convert the Latin-1 file stopped with “illegal input sequence at position 3,” the same byte Python complained about. Converting with iconv -f latin1 -t utf-8 fixes the file properly, by turning the single e9 byte into the two-byte UTF-8 sequence.

Same look, different bytes

One last trap survives even perfect decoding. The é in “café” can be stored as the single code point U+00E9 or as a plain e followed by a combining accent, U+0301. Python’s 'é' == 'é' returned False, and the combined form has a length of 2. Running both through unicodedata.normalize('NFC', ...) made them equal. Two strings that look identical on screen can fail a comparison, a search or a database unique check until they’re normalized to the same form.

The rule underneath all of it

The WHATWG standard states the modern answer plainly: “New protocols and formats, as well as existing formats deployed in new contexts, must use the UTF-8 encoding exclusively.” Mojibake mostly lives at the seams where an older system meets a newer one, which is where an explicit encoding on every read and write pays for itself.

Sources

  • RFC 3629, UTF-8, a transformation format of ISO 10646: https://www.rfc-editor.org/rfc/rfc3629
  • UTF-8, UTF-16, UTF-32 & BOM FAQ, Unicode Consortium: https://www.unicode.org/faq/utf_bom.html
  • Encoding Standard, WHATWG: https://encoding.spec.whatwg.org/
  • codecs: Codec registry and base classes, Python documentation: https://docs.python.org/3/library/codecs.html

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *