参考: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. C#中IP地址转换为数值的方法

    任何语言都通用的方法转换 IP 地址 a.b.c.d ==> a***+b**+c*+d ===> *(c+*(b+*a)) +d 示例: ***+**+*+ ===> *( +*( ...

  2. Measuring PostgreSQL Checkpoint Statistics

    Checkpoints can be a major drag on write-heavy PostgreSQL installations. The first step toward ident ...

  3. 自然语言处理3.3——使用Unicode进行文字处理

    全世界有多种语言,经常需要应用程序处理不同的语言和字符集.下面将介绍如何利用Unicode处理使用非ASCII字符集文字. 1.什么是Unicode Unicode支持一百万种以上的字符,每一个字符分 ...

  4. 学习tornado:安全

    http://blog.csdn.net/siddontang/article/details/18053915

  5. kubernetes集群部署

    鉴于Docker如此火爆,Google推出kubernetes管理docker集群,不少人估计会进行尝试.kubernetes得到了很多大公司的支持,kubernetes集群部署工具也集成了gce,c ...

  6. python爬虫之BeautifulSoup

    爬虫有时候写正则表达式会有假死现象 就是正则表达式一直在进行死循环查找 例如:https://social.msdn.microsoft.com/forums/azure/en-us/3f4390ac ...

  7. js/jquery 操作document对象

    一.获取对象 //js获取的是dom对象,jquery获取的是jquery对象 //jquery对象可以输出dom对象,索引方式输出dom对象,eq()[]方式输出dom对象;eq()输出jquery ...

  8. Angular学习(1)

    天天都是hello world,老子玩1+1. 最简单的例子,见证无聊的时刻: <!DOCTYPE html> <html> <head> <title> ...

  9. IOS开发-UIBarButtonItem系统自带图标总结

    1.这四个返回的是后面的单词. UIBarButtonSystemItemDone UIBarButtonSystemItemCancel UIBarButtonSystemItemEdit UIBa ...

  10. lucene、lucene.NET详细使用与优化详解

    lucene.lucene.NET详细使用与优化详解 2010-02-01 13:51:11 分类: Linux 1 lucene简介1.1 什么是luceneLucene是一个全文搜索框架,而不是应 ...