+ -
当前位置:首页 → 问答吧 → 中文编码(decode)的问题

中文编码(decode)的问题

时间:2010-08-14

来源:互联网

decode文档这样描述:
When you run $string = decode("utf8", $octets) , then $string may not be equal to $octets. Though they both contain the same data, the UTF8 flag for $string is on unless $octets entirely consists of ASCII data (or EBCDIC on EBCDIC machines). See The UTF8 flag below.

网上也有中文描述:
$string = decode(ENCODING, $octets [, CHECK]) 把字符串从其他编码转成utf8编码, 并开启utf8 flag, 不过有个例外就是, 如果字符串是仅仅ascii编码或EBCDIC编码的话, 不开启utf8 flag.

我测试了一下:
#!/usr/bin/perl -w
use Encode;
$str0="cn";
$str=Encode::decode("utf8",$str0);
#Encode::_utf8_off($str);

print "str0:Yes\n" if(Encode::is_utf8($str0));
print "str:Yes\n" if(Encode::is_utf8($str));

输出结果:
str:Yes

难道asicc也开启了utf8 flag?

还有个问题就是:
use utf8;
use encoding "utf8";
有什么区别?

作者: better0332   发布时间: 2010-08-14

use utf8 是开启 perl unicode的支持不会去转换的。
而 use encoding "utf8" 则是将代码转换成 utf-8的去处理的
如 两个 unicode的字符连接后如果不使用

use encoding "utf8" 那么结果就是 latin-1
以上是我的理解了
perldoc上有说明的

作者: redskywy   发布时间: 2010-08-14