免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 15901 | 回复: 23
打印 上一主题 下一主题

Encode模块中,encode()和decode()的区别是什么啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-28 16:20 |只看该作者 |倒序浏览
$octets = encode("iso-8859-1", $string);
$string = decode("iso-8859-1", $octets);
由于小弟的英文不太好,实在看不明白encode()和decode()的区别是什么,请指教。

论坛徽章:
0
2 [报告]
发表于 2005-11-28 16:31 |只看该作者
$octets = encode("iso-8859-1", $string);把一个串从perl内部格式转为iso-8859-1格式
$string = decode("iso-8859-1", $octets);转换ISO-8859-1数据为perl内部格式串

论坛徽章:
0
3 [报告]
发表于 2005-11-28 16:51 |只看该作者
请问perl内部格式是什么啊?
$data = encode("utf8", decode("iso-8859-1", $data));
以上语句的意思是不是先将iso-8859-1数据转换成perl内部格式,再将perl内部格式转换成utf8格式,实际上的意思就是将iso-8859-1数据转换成utf8数据,对吗?

论坛徽章:
0
4 [报告]
发表于 2005-11-28 19:00 |只看该作者
原帖由 xxgsky 于 2005-11-28 16:51 发表
请问perl内部格式是什么啊?
$data = encode("utf8", decode("iso-8859-1", $data));
以上语句的意思是不是先将iso-8859-1数据转换成perl内部格式,再将perl内部格式转换成utf8格式,实际上 ...


perl内部就是utf-8格式编码的。encode与decode与字面理解的意义正好相反,它们实际上指的是perl的内在data表现方式,encode通常unset perl内部的utf-8 flag,而decode则设置这个flag。所以decode实际上创建utf-8字符串,encode则反之。例如,如下语句设置输出句柄的编码:

open my $out, ">:encoding(Latin1)"; #or
binmode(STDOUT, ":encoding(Big5)");

论坛徽章:
0
5 [报告]
发表于 2005-11-29 08:53 |只看该作者
perl内部就是utf-8格式编码的。

perl的内部格式好像不是严格的utf-8编码哦,而是utf8编码,这两者好像是有区别的
UTF-8 vs. utf8
  ....We now view strings not as sequences of bytes, but as sequences
  of numbers in the range 0 .. 2**32-1 (or in the case of 64-bit
  computers, 0 .. 2**64-1) -- Programming Perl, 3rd ed.
That has been the perl's notion of UTF-8 but official UTF-8 is more strict; Its ranges is much narrower (0 .. 10FFFF), some sequences are not allowed (i.e. Those used in the surrogate pair, 0xFFFE, et al).

Now that is overruled by Larry Wall himself.

  From: Larry Wall <larry@wall.org>
  Date: December 04, 2004 11:51:58 JST
  To: perl-unicode@perl.org
  Subject: Re: Make Encode.pm support the real UTF-8
  Message-Id: <20041204025158.GA28754@wall.org>
  
  On Fri, Dec 03, 2004 at 10:12:12PM +0000, Tim Bunce wrote:
  : I've no problem with 'utf8' being perl's unrestricted uft8 encoding,
  : but "UTF-8" is the name of the standard and should give the
  : corresponding behaviour.
  
  For what it's worth, that's how I've always kept them straight in my
  head.
  
  Also for what it's worth, Perl 6 will mostly default to strict but
  make it easy to switch back to lax.
  
  Larry
Do you copy? As of Perl 5.8.7, UTF-8 means strict, official UTF-8 while utf8 means liberal, lax, version thereof. And Encode version 2.10 or later thus groks the difference between UTF-8 and C"utf8".

  encode("utf8",  "\x{FFFF_FFFF}", 1); # okay
  encode("UTF-8", "\x{FFFF_FFFF}", 1); # croaks
UTF-8 in Encode is actually a canonical name for utf-8-strict. Yes, the hyphen between "UTF" and "8" is important. Without it Encode goes "liberal"

  find_encoding("UTF-8")->name # is 'utf-8-strict'
  find_encoding("utf-8")->name # ditto. names are case insensitive
  find_encoding("utf8")->name  # ditto. "_" are treated as "-"
  find_encoding("UTF8")->name  # is 'utf8'.

这段选自Encode模块的说明文档

论坛徽章:
0
6 [报告]
发表于 2005-11-29 08:56 |只看该作者
en ,utf8 & utf-8是不同的吧,我总把它们当一样的,

论坛徽章:
0
7 [报告]
发表于 2005-11-29 11:16 |只看该作者
原帖由 兰花仙子 于 2005-11-28 19:00 发表


例如,如下语句设置输出句柄的编码:

open my $out, ">:encoding(Latin1)"; #or
binmode(STDOUT, ":encoding(Big5)");

请问仙子,哪两句语句是什么意思呢?

论坛徽章:
0
8 [报告]
发表于 2005-11-29 11:19 |只看该作者
原帖由 xxgsky 于 2005-11-29 11:16 发表

请问仙子,哪两句语句是什么意思呢?


把输出句柄的编码设为拉丁文或BIG5

论坛徽章:
0
9 [报告]
发表于 2005-11-29 15:27 |只看该作者
原帖由 怒剑狂啸 于 2005-11-28 16:31 发表
$octets = encode("iso-8859-1", $string);把一个串从perl内部格式转为iso-8859-1格式
$string = decode("iso-8859-1", $octets);转换ISO-8859-1数据为perl内部格式串

请教一下,如果
print $octets;把标量$octets打印在屏幕上会不会乱码呢?
又或者如果对标量$octets进行操作,因为标量$octets并不是perl内部格式,会不会引起其它意思不到的错误呢?
为什么我在读取UTF8文件时,写如下语句(WINDOW环境)
$data = encode("gbk", decode("utf8", $_));
print $data;
在屏幕上输出的正确的编码.为什么他最后打出GBK的编码而不会错呢?是不是因为我系统是WINDOW,所以按GBK格式去读取UTF8格式的文件,最后经过两次转换……(过程究竟是怎么样的呢?)
这些关系我没有搞明白,越想越乱,望大侠指教.

论坛徽章:
0
10 [报告]
发表于 2005-11-29 16:07 |只看该作者
print $octets;把标量$octets打印在屏幕上会不会乱码呢?

要看你操作系统是否支持该编码?
为什么我在读取UTF8文件时,写如下语句(WINDOW环境)
$data = encode("gbk", decode("utf8", $_));
print $data;
在屏幕上输出的正确的编码.为什么他最后打出GBK的编码而不会错呢?

为什么会错哩?
你也可以这样做
  1. $data=from_to($_,"utf8","gbk")
复制代码

效果是一样的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP