小编ssd*_*ssd的帖子

如何转换存储的错误编码数据?

我的Perl应用程序和MySQL数据库现在正确处理传入的UTF-8数据,但我必须转换预先存在的数据.有些数据似乎已编码为CP-1252,在编码为UTF-8并存储在MySQL中之前未进行解码.我已经阅读了O'Reilly文章将latin1中的MySQL数据转换为utf8 utf-8,但尽管它经常被引用,但它并不是一个明确的解决方案.

我看过Encode :: DoubleEncodedUTF8Encoding :: FixLatin,但是我的数据都没有.

这是我到目前为止所做的:

#Return the $bytes from the DB using BINARY()
my $characters = decode('utf-8', $bytes);
my $good = decode('utf-8', encode('cp-1252', $characters));
Run Code Online (Sandbox Code Playgroud)

这解决了大多数情况,但如果针对proplerly编码的记录运行,它会破坏它们.我尝试过使用Encode :: GuessEncode :: Detect,但他们无法区分正确编码和错误编码的记录.所以我只是在转换后找到\ x {FFFD}字符时撤消转换.

但是,有些记录只是部分转换.这是一个左卷曲引号被正确转换的例子,但正确的卷曲引号被破坏了.

perl -CO -MEncode -e 'print decode("utf-8", encode("cp-1252", decode("utf-8", "\xC3\xA2\xE2\x82\xAC\xC5\x93four score\xC3\xA2\xE2\x82\xAC\xC2\x9D")))'
Run Code Online (Sandbox Code Playgroud)

并且这是一个右单引号未转换的示例:

perl -CO -MEncode -e 'print decode("utf-8", encode("cp-1252", decode("utf-8", "bob\xC3\xAF\xC2\xBF\xC2\xBDs")))'
Run Code Online (Sandbox Code Playgroud)

我还在这里处理双重编码数据吗?我还需要做些什么来转换这些记录?

mysql perl encoding utf-8

5
推荐指数
1
解决办法
3346
查看次数

标签 统计

encoding ×1

mysql ×1

perl ×1

utf-8 ×1