参考:http://daimajishu.iteye.com/blog/959239
不过具测试,也有错误:
原文如下:

# author: jiangyujie
use utf8;  ##在最后一个例子,这里面不能有use utf8;
use Encode;
use URI::Escape;

$\ = "\n";

#从unicode得到utf8编码
$str = '%u6536';
$str =~ s/\%u([0-9a-fA-F]{4})/pack("U",hex($1))/eg;
$str = encode( "utf8", $str );
print uc unpack( "H*", $str );

# 从unicode得到gb2312编码
$str = '%u6536';
$str =~ s/\%u([0-9a-fA-F]{4})/pack("U",hex($1))/eg;
$str = encode( "gb2312", $str );
print uc unpack( "H*", $str );

# 从中文得到utf8编码
$str = "收";
print uri_escape($str);

# 从utf8编码得到中文
$utf8_str = uri_escape("收");
print uri_unescape($str);

# 从中文得到perl unicode
utf8::decode($str);
@chars = split //, $str;
foreach (@chars) {
printf "%x ", ord($_);
}

# 从中文得到标准unicode
$a = "汉语";
$a = decode( "utf8", $a );
map { print "\\u", sprintf( "%x", $_ ) } unpack( "U*", $a );

# 从标准unicode得到中文
$str = '%u6536';
$str =~ s/\%u([0-9a-fA-F]{4})/pack("U",hex($1))/eg;
$str = encode( "utf8", $str );
print $str;

# 从perl unicode得到中文
my $unicode = "\x{505c}\x{8f66}";
print encode( "utf8", $unicode );   ##据我测试,这里有错误!应该这样写: utf8::encode($unicode); print $unicode;

======================下面是我的测试

1)编码中文
[root@tts177:/tmp]$more uuu.pl
#!/usr/bin/perl
use warnings;
use Data::Dumper;
use URI::Escape;

$utf8_str = uri_escape("收");

print $utf8_str;
[root@tts177:/tmp]$
[root@tts177:/tmp]$./uuu.pl
%E6%94%B6[root@tts177:/tmp]$
[root@tts177:/tmp]$

2)解码url
[root@tts177:/tmp]$more uuu.pl
#!/usr/bin/perl
use warnings;
use Data::Dumper;
use URI::Escape;

$utf8_str = uri_escape("收");

print $utf8_str;
[root@tts177:/tmp]$
[root@tts177:/tmp]$./uuu.pl
%E6%94%B6[root@tts177:/tmp]$
[root@tts177:/tmp]$
[root@tts177:/tmp]$
[root@tts177:/tmp]$more uuu.pl
#!/usr/bin/perl
use warnings;
use Data::Dumper;
use URI::Escape;

$str = "%E6%94%B6";

print uri_unescape($str);
[root@tts177:/tmp]$
[root@tts177:/tmp]$./uuu.pl
收[root@tts177:/tmp]$
[root@tts177:/tmp]$

Perl中文/unicode/utf8/GB2312之间的转换的更多相关文章

  1. 各种字符编码方式详解及由来(ANSI,UNICODE,UTF-8,GB2312,GBK)

    一直对字符的各种编码方式懵懵懂懂,什么ANSI UNICODE UTF-8 GB2312 GBK DBCS UCS……是不是看的很晕,假如您细细的阅读本文你一定可以清晰的理解他们.Let's go! ...

  2. UTF-8 GBK UTF8 GB2312之间的区别和关系

    UTF-8 GBK UTF8 GB2312之间的区别和关系     UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符 ...

  3. 关于python中的编码:unicode, utf-8, gb2312

    计算机早期是只支持ASCII码的,经过long long的发展,出现了这些支持世界上各种语言字符的编码:unicode, utf-8, gb2312. 对于unicode, utf-8, gb2312 ...

  4. python中unicode, hex, bin之间的转换

    python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...

  5. Ansi、Unicode、UTF8字符串之间的转换和写入文本文件

    转载请注明出处http://www.cppblog.com/greatws/archive/2008/08/31/60546.html 最近有人问我关于这个的问题,就此写一篇blog Ansi字符串我 ...

  6. python2 中 unicode 和 str 之间的转换及与python3 str 的区别

    在python2中字符串分为unicode 和 str 类型 Str To Unicode 使用decode(), 解码 Unicode To Str 使用encode(), 编码 返回数据给前端时需 ...

  7. C++转换unicode utf-8 gb2312编码

    windows开发环境下用VC++6.0 对unicode .utf-8. gb2312 三种编码格式之间的转换方法: #include <iostream> #include <s ...

  8. 【转】关于字符编码,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…)

    转载地址:http://www.imkevinyang.com/2010/06/%E5%85%B3%E4%BA%8E%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81%EF%BC ...

  9. 关于字符编码,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…)

    字符编码的问题看似很小,经常被技术人员忽视,但是很容易导致一些莫名其妙的问题.这里总结了一下字符编码的一些普及性的知识,希望对大家有所帮助. 还是得从ASCII码说起 说到字符编码,不得不说ASCII ...

随机推荐

  1. java 中的断言assert的使用

    一.assertion的意义和用法 J2SE 1.4在语言上提供了一个新特性,就是assertion功能,它是该版本在Java语言方面最大的革新. 从理论上来说,通过 assertion方式可以证明程 ...

  2. 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)

    本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...

  3. VMware Workstation pro 12下载以及序列号

    VMware Workstation 12序列号:下载地址:https://download3.vmware.com/software/wkst/file/VMware-workstation-ful ...

  4. 客户端TortoiseSVN的安装及使用方法

    一.客户端TortoiseSVN的安装 运行TortoiseSVN程序,点击Next,下面的截图顺序即为安装步骤: 图1: 图2: 图3: 图4: 点击Finish按钮后会提示重启系统,其实不重启也没 ...

  5. Eclipse给方法分配足够的内存

    junit测试 VM parameters -Xmx1024M -XX:PermSize=128m -XX:MaxPermSize=256m

  6. 导出resource文件的的资源

    写个小工具,方便一次性将resource文件中的资源导出,不然反编译后一个个找,真是太麻烦了. using System;using System.Collections.Generic;using  ...

  7. 【Struts2学习笔记-8】Struts2实现json数据的返回

    需要的jar包 struts2-json-plugin-2.3.12.jar xwork-core-2.3.16.3.jar struts.xml 来自为知笔记(Wiz) 附件列表 IMG_20150 ...

  8. 使用仓库管理器——Sonatype Nexus的九大理由

    目前有很多组织使用了一些工具依赖于Maven仓库,但他们并没有采用一个仓库管理器,对于这一点我十分惊讶.可能没人提出来这一点,没人站出来告诉别人使用一个仓库管理器能带来什么好处.我经常能从很多不使用M ...

  9. request的生命周期

    有如下功能: 从index.jsp页面点击超链接进入TestServlet服务器,TestServlet服务器再请求转发到test.jsp. 在index.jsp里设置了request的attribu ...

  10. Ubuntu Server上的LVM配置

    在安装Linux的时候,通常遇到的一个比较头痛的问题就是分区,到底每个区该分多少,用了一段时间之后,某个分区又不够用了,该怎么办?如果是普通的服务器,那一切都好说,大不了就关机重新划分分区嘛,但是对于 ...