Menu

Why a UTF-8 charset meta tag cannot fix mojibake already saved in HTML
📰

Why a UTF-8 charset meta tag cannot fix mojibake already saved in HTML

DEV Community: html·Aleksey Kulakov·14 days ago
#MeV7RWvB
#devto2#devto#dev#bytes#every
Long article (665 words) — no AI summary yet.
Reading 0:00
15s threshold

Your file can be valid UTF-8 and still display Café . That happens when the original UTF-8 bytes for Café were decoded as Windows-1252, then the resulting wrong characters were saved as UTF-8. The browser is now reading the saved bytes correctly; the text itself is already wrong.

Problem

The key detail is that this failure leaves a recognizable trace. The original UTF-8 bytes for é are C3 A9 . Read as Windows-1252, those bytes become the characters é . Saving that text as UTF-8 writes new bytes for those two characters. A <meta charset="UTF-8"> declaration can correctly describe the new file, but it cannot infer that é used to be é .

# Text in the damaged file: Café

# The inverse of the mistaken decode: "Café".encode("cp1252").decode("utf-8") # 'Café'

Enter fullscreen mode

Exit fullscreen mode

This is not a general rule that every occurrence of à is corrupt. The same characters can appear legitimately, and not every mojibake sequence has this exact history.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.