【问题标题】:Perl UTF8 to UTF16 conversion error - wide charactersPerl UTF8 到 UTF16 转换错误 - 宽字符
【发布时间】:2013-03-28 04:31:58
【问题描述】:

谁能告诉我为什么这个 sn-p 失败并出现以下错误?我也试过 utf8::downgrade() 在调用 from_to() 之前没有成功。使用 Perl 5.14.2。

有什么想法吗??

代码:

use Encode qw(from_to);
use HTML::Entities;

$s = "มหั&#3624";
$foo = decode_entities($s);
print "is foo UTF8? ", utf8::is_utf8($foo), "\n";
from_to($foo, 'UTF-8', 'UTF-16');

输出:

is foo UTF8? 1
Cannot decode string with wide characters at /usr/lib/perl/5.14/Encode.pm line 194.

【问题讨论】:

    标签: perl unicode


    【解决方案1】:

    首先,utf8::is_utf8 并没有按照你的想法去做。它提供了有关字符串内部存储的详细信息,nothing you should ever need to check

    问题是您的字符串未使用 UTF-8 编码。它根本没有编码。 decode_entities 接受并返回一个解码字符串,一个 Unicode 代码点字符串。

    你可以使用

    encode('UTF-16', decode_entities(decode('UTF-8', $foo)))
    

    【讨论】:

      猜你喜欢
      • 2012-11-04
      • 2020-09-18
      • 2014-02-24
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      相关资源
      最近更新 更多