免费注册 查看新帖 |

Chinaunix

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

decode,encode的问题,大家帮忙看看 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-12 16:21 |只看该作者 |倒序浏览
$name=encode("gb2312",decode("utf8",$name));
报错Cannot decode string with wide characters
$name本身是utf8编码,我想转成gb2312编码,是这么写吧?为什么报错呢?

论坛徽章:
0
2 [报告]
发表于 2006-09-12 17:36 |只看该作者
原帖由 m0925j 于 2006-9-12 16:21 发表
$name=encode("gb2312",decode("utf8",$name));
报错Cannot decode string with wide characters
$name本身是utf8编码,我想转成gb2312编码,是这么写吧?为什么报错呢?


Hi,
put your raw string from $name (before decode) here, otherwise nobody can find
the error.

PS.: ref.to http://bbs.chinaunix.net/viewthread.php?tid=711103&extra=page%3D1

regards, ulmer

[ 本帖最后由 ulmer 于 2006-9-12 17:58 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-09-13 09:35 |只看该作者
$name在txt里面打印出来是“濮撳悕”,在ultraedit里面显示是“姓名”

论坛徽章:
0
4 [报告]
发表于 2006-09-13 09:55 |只看该作者
use Encode;
use Encode::CN;

$a = '濮撳悕';
$b = encode("euc-cn",decode_utf8($a));
print $b;

论坛徽章:
0
5 [报告]
发表于 2006-09-13 11:08 |只看该作者
use Encode;
$a = '绉诲姩鐢佃瘽';
$b = encode("gb2312",decode("utf8",$a));
print $b;
用我前面的写法直接这样运行一样可以
那$name是从别人那里接受的一个字符串里面获取的,我把它print到一个txt里面的时候还会出警告Wide character in print

论坛徽章:
0
6 [报告]
发表于 2006-09-13 13:33 |只看该作者
接收过来的时候是UTF8还是GB的?

论坛徽章:
0
7 [报告]
发表于 2006-09-13 14:24 |只看该作者
UTF8的

论坛徽章:
0
8 [报告]
发表于 2006-09-13 14:25 |只看该作者
use SOAP::Lite;
use Encode;
use strict;
use warnings;
my $login_result= SOAP::Lite
  -> uri('http://192.168.61.172:8090/ca')
  -> proxy('http://192.168.61.172:8090/ca')
  -> ca_login(' 1',' id1',' 123456',' 0',' 192.168.61.50')
  -> result;
my ($sid)=$login_result=~m/<casid type="String">(.*)<\/casid>/s;
my $validate_result=SOAP::Lite
            -> uri('http://192.168.61.172:8090/ca')
            -> proxy('http://192.168.61.172:8090/ca')
            -> ca_validatewithinfo(' '."$sid",' id1',' '.'192.168.60.52')
            -> result;
my ($name)=$validate_result=~m/<caU_NAME type="String">(.*)<\/caU_NAME>/s;
  #$name=encode("gb2312",decode("utf8","$name"));
  #$name=encode("gb2312",decode("utf8","濮撳悕"));
print $name;

现在这样运行的结果是
Wide character in print at 2.pl line 19.
濮撳悕

如果只把第一个注释去掉运行就报错
Cannot decode string with wide characters at C:/Perl/lib/Encode.pm line 166.

如果只把第二个注释去掉运行就正确打印
姓名

这是为什么啊,搞不明白了~~

论坛徽章:
0
9 [报告]
发表于 2006-09-13 16:42 |只看该作者
摘自 中国Perl协会 用户推广组   cnhacktnt  2006-09-13 16:07
http://perlchina.sun126.com/cgi-bin/ccb/index.cgi
如果没错的话,$name=encode("gb2312",$name); 应该就可以了,$validate_result 的返回值为utf8编码,且SOAP::Lite已经通知了perl 变量 $validate_result 的返回值为utf8编码,相当于 SOAP::Lite 已经做了 decode 的工作,decode的工作像这样


utf8编码字串 --(作为字节流)--> perl不知道它是什么编码 ---> decode ('utf8',$foo) ---> perl知道他是utf8编码了,并将其转换为对应的unicode(两个字节)来操作(但实际存储的时候仍是utf8编码)

之所以会出现 Wide character in print at 2.pl line 19. 这样的结果,是因为perl知道你输出的变量字符串是utf8编码的多字节字符,你应该通过encode函数将该变量转换成字节流,这样perl就不会出现这个警告

综上,可以知道,perl对待外来的数据一律当作字节流处理,通过 decode 函数可以告诉 perl 当前字节流是什么编码,这时 perl 就会把他按照原来对应的编码方式内部解码为 unicode 来操作(此时这串字节流已经被perl当作是有一定意义的unicode字符串来理解了),当你要将操作完的字符串输出的时候,应该将其用encode函数按照相应的编码方式打包成字节流再输出.


perlchina上一位兄弟的回复,搞定了,呵呵

论坛徽章:
0
10 [报告]
发表于 2006-09-13 16:54 |只看该作者
原帖由 m0925j 于 2006-9-13 14:25 发表
...
my $validate_result=SOAP::Lite
            -> uri('http://192.168.61.172:8090/ca')
            -> proxy('http://192.168.61.172:8090/ca')
            -> ca_validatewithinfo(' '."$sid",' id1',' '.'192.168.60.52')
            -> result;
my ($name)=$validate_result=~m/<caU_NAME type="String">(.*)<\/caU_NAME>/s;


Are you sure $validate_result returned from SOAP::Lite valide UTF8?
what is output: print  $validate_result; ?
what is output: print $name; ?
Before de(en)coding, check the string using Encode::is_utf8(STRING [, CHECK])
(Perl version 5.8.1 required).
If  $validate_result is well-formatted xml structure, using some CPAN modules for
XML parse is anther choice too.

Try again, --ulmer
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP