신영 안
Run Code Online (Sandbox Code Playgroud)
上面是html,下面是代码。这是一个名字吗?这是什么意思?
신옠안
Run Code Online (Sandbox Code Playgroud)
你有一个双 Mojibake,数据因使用不正确的编解码器而受损。
它实际上是韩文,一个名字:
?? ?
Run Code Online (Sandbox Code Playgroud)
或者,如果使用 HTML 实体,这应该被编码为
신영 안
Run Code Online (Sandbox Code Playgroud)
它翻译成英文为Shin-Young An。
当编码为 UTF-8,并按输入代码点分组,然后使用十六进制数字显示时,您将得到:
ec 8b a0
ec 98 81
20
ec 95 88
Run Code Online (Sandbox Code Playgroud)
要产生您拥有的输出,必须有人拥有:
使用Windows 代码页 1252解码上述 UTF-8 数据,产生
ì‹<A0>ì˜<81> 안
Run Code Online (Sandbox Code Playgroud)
(<A0>不间断空格字符在哪里,并且<81>是一个无效的 CP1252 字节,但这在许多解码器中经常被忽略;我已将它们包含在此表示法中,因为否则它们将无法打印)
再次将结果混乱编码为 UTF-8,为您提供以下字节值:
c3 ac e2 80 b9 c2 a0
c3 ac cb 9c c2 81
20
c3 ac e2 80 a2 cb 86
Run Code Online (Sandbox Code Playgroud)
(分组与上面正确的 UTF-8 匹配)
使用相同的 Windows CP1252 编解码器第二次解码这些 UTF-8 字节,这次产生:
ì‹Â<A0>ì˜Â<81> 안
Run Code Online (Sandbox Code Playgroud)
(在<A0>和<81>字符上有相同的注释)
最后将结果字符编码为 HTML 实体:
신영 안
Run Code Online (Sandbox Code Playgroud)如果您安装了 Python,那么该ftfy库可以通过一个步骤“修复”这样的文本:
>>> import ftfy
>>> sample = '신영 안'
>>> ftfy.ftfy(sample)
'?? ?'
Run Code Online (Sandbox Code Playgroud)
我用那个库来告诉我使用了哪些编解码器,以及使用它的草率 CP1252 解码器来生成上面的解码。
例如,对于您的输入,我使用了:
>>> ftfy.fixes.fix_encoding_and_explain(ftfy.fixes.unescape_html(sample))
('?? ?', [('encode', 'sloppy-windows-1252', 0), ('decode', 'utf-8', 0), ('encode', 'sloppy-windows-1252', 0), ('decode', 'utf-8', 0)])
Run Code Online (Sandbox Code Playgroud)
看到维修计划,把它倒过来解释一下最初是如何生产Mojibake的。