<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aleksey Kulakov</title>
    <description>The latest articles on DEV Community by Aleksey Kulakov (@despe4er).</description>
    <link>https://dev.to/despe4er</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3047462%2Fb08fb6c3-ba71-43dc-94cf-645242a4c2bb.jpg</url>
      <title>DEV Community: Aleksey Kulakov</title>
      <link>https://dev.to/despe4er</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/despe4er"/>
    <language>en</language>
    <item>
      <title>Why a UTF-8 charset meta tag cannot fix mojibake already saved in HTML</title>
      <dc:creator>Aleksey Kulakov</dc:creator>
      <pubDate>Sat, 21 Nov 2026 21:44:18 +0000</pubDate>
      <link>https://dev.to/despe4er/why-a-utf-8-charset-meta-tag-cannot-fix-mojibake-already-saved-in-html-334n</link>
      <guid>https://dev.to/despe4er/why-a-utf-8-charset-meta-tag-cannot-fix-mojibake-already-saved-in-html-334n</guid>
      <description>&lt;p&gt;Your file can be valid UTF-8 and still display &lt;code&gt;CafÃ©&lt;/code&gt;. That happens when the original UTF-8 bytes for &lt;code&gt;Café&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Text in the damaged file:
CafÃ©

# The inverse of the mistaken decode:
"CafÃ©".encode("cp1252").decode("utf-8")
# 'Café'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a general rule that every occurrence of &lt;code&gt;Ã&lt;/code&gt; is corrupt. The same characters can appear legitimately, and not every mojibake sequence has this exact history. The conversion is justified only when you have evidence that this particular text was UTF-8 misread as Windows-1252. Compare it with a trusted source, translation memory, or an unaffected copy before changing anything.&lt;/p&gt;

&lt;p&gt;The distinction between a declaration and a conversion is the central point in the &lt;a href="https://htmltranslate.com/blog/fix-garbled-characters-mojibake-html-utf8/" rel="noopener noreferrer"&gt;walkthrough on diagnosing and repairing mojibake in HTML&lt;/a&gt;. For this specific pattern, think of the repair as reversing one known, mistaken decoding step—not as changing the document's charset.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix
&lt;/h2&gt;

&lt;p&gt;Repair the known damaged text, not the entire file. A broad "convert every string containing &lt;code&gt;Ã&lt;/code&gt;" pass can alter legitimate content. Even applying the inverse conversion to every character in a document can fail on characters outside Windows-1252 or change unrelated text. Start with an exact, reviewed phrase and make the script fail if that phrase is absent or appears an unexpected number of times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;

&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"page.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"page.repaired.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;damaged&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"CafÃ©"&lt;/span&gt;
&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Café"&lt;/span&gt;

&lt;span class="c1"&gt;# Reverse the specific cp1252-read / UTF-8-bytes mistake.&lt;/span&gt;
&lt;span class="n"&gt;repaired&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;damaged&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cp1252"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;repaired&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Unexpected repair result: {repaired!r}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;damaged&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Expected one reviewed match, found {count}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;damaged&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;repaired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Wrote {output}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Writing to a separate output file gives you a simple rollback path and keeps the original available for comparison. If the same reviewed phrase appears in multiple places, decide explicitly whether every occurrence should change; adjust the count check only after reviewing those locations. For a larger batch, keep a mapping of confirmed source strings to repaired strings and report every replacement instead of silently sweeping for suspicious markers. The &lt;a href="https://github.com/htmltranslate/repair_mojibake" rel="noopener noreferrer"&gt;repair_mojibake repository&lt;/a&gt; is useful context for treating this as a targeted conversion rather than a charset declaration change.&lt;/p&gt;

&lt;p&gt;After the edit, inspect the output as text and confirm it still decodes as UTF-8. Then check the served page, not just a local editor preview. A correct response charset is still important, but it answers a different question: how should the browser decode the bytes it receives? It does not establish that the characters stored in those bytes are the intended ones.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Production rule: keep the original file and write repairs to a separate output until the changed text has been reviewed.&lt;/p&gt;

&lt;p&gt;Production rule: never apply the cp1252-to-UTF-8 reversal to every occurrence of &lt;code&gt;Ã&lt;/code&gt; without confirming the source and expected result.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once the content is correct, keep the normal pipeline consistent: save the HTML as UTF-8 and serve it with a matching UTF-8 charset. That prevents a repeat of the mistake; it does not replace the targeted repair for text that was already misdecoded and saved.&lt;/p&gt;

</description>
      <category>html</category>
      <category>utf8</category>
      <category>i18n</category>
      <category>python</category>
    </item>
  </channel>
</rss>
