Perl中文/unicode/utf8/GB2312之间的转换
参考: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之间的转换的更多相关文章
- 各种字符编码方式详解及由来(ANSI,UNICODE,UTF-8,GB2312,GBK)
一直对字符的各种编码方式懵懵懂懂,什么ANSI UNICODE UTF-8 GB2312 GBK DBCS UCS……是不是看的很晕,假如您细细的阅读本文你一定可以清晰的理解他们.Let's go! ...
- UTF-8 GBK UTF8 GB2312之间的区别和关系
UTF-8 GBK UTF8 GB2312之间的区别和关系 UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符 ...
- 关于python中的编码:unicode, utf-8, gb2312
计算机早期是只支持ASCII码的,经过long long的发展,出现了这些支持世界上各种语言字符的编码:unicode, utf-8, gb2312. 对于unicode, utf-8, gb2312 ...
- python中unicode, hex, bin之间的转换
python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
转载请注明出处http://www.cppblog.com/greatws/archive/2008/08/31/60546.html 最近有人问我关于这个的问题,就此写一篇blog Ansi字符串我 ...
- python2 中 unicode 和 str 之间的转换及与python3 str 的区别
在python2中字符串分为unicode 和 str 类型 Str To Unicode 使用decode(), 解码 Unicode To Str 使用encode(), 编码 返回数据给前端时需 ...
- C++转换unicode utf-8 gb2312编码
windows开发环境下用VC++6.0 对unicode .utf-8. gb2312 三种编码格式之间的转换方法: #include <iostream> #include <s ...
- 【转】关于字符编码,你所需要知道的(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 ...
- 关于字符编码,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…)
字符编码的问题看似很小,经常被技术人员忽视,但是很容易导致一些莫名其妙的问题.这里总结了一下字符编码的一些普及性的知识,希望对大家有所帮助. 还是得从ASCII码说起 说到字符编码,不得不说ASCII ...
随机推荐
- 如何给EDIUS添加区域性马赛克
我们平时观看视频时会经常看到画面上的一部分信息被用马赛克来遮盖,那在使用EDIUS制作编辑视频时,这一效果要如何做到呢?下面,小编就来给大家讲讲如何给EDIUS区域添加马赛克吧. 1.首先在特效面板中 ...
- 在svg里面画虚线
使用stroke-dasharray="3 2" 属性,其中3和2分别表示画的长度和间隙的长度 比如 <line x1="0" y1="5&q ...
- Problem A+B(Big Integer)
/*======================================================================== Problem A+B(Big Integer) ...
- Linux中的free命令
解释一下Linux上free命令的输出. 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO(Free Output).例如: FO[2][ ...
- shiro权限验证标签
实例: spring-shiro.xml /admin/repairType/index = roles["ROLE_ADMIN"] /admin/user=roles[" ...
- Android SingleTask与SingleInstance的区别
Android SingleTask与SingleInstance的区别 原文地址 现有2个项目,taskA.taskB.taskA负责调用taskB中指定的界面. taskB中有3个界面,a.b.c ...
- 动态代理到基于动态代理的AOP
动态代理,是java支持的一种程序设计方法. 动态代理实现中有两个重要的接口和类,分别是InvocationHandler(interface),Proxy(class). 要实现动态代理,必须要定义 ...
- struts checkbox选中
<input type="checkbox" id="something" name="something" /> ↓ < ...
- Asp.Net MVC 路由 - Asp.Net 编程 - 张子阳
http://cache.baiducontent.com/c?m=9d78d513d98316fa03acd2294d01d6165909c7256b96c4523f8a9c12d522195646 ...
- 【linux】rpm常见命令
查看包依赖模块:http://www.rpmfind.net/ rpm -ivh [包名] :安装rpm包 rpm -e [包名] :删除rpm包 rpm -aq [包名] :查询rpm包 rp ...