加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl 转化 utf8/unicode/gb2312

发布时间:2020-12-15 23:40:42 所属栏目:大数据 来源:网络整理
导读:use utf8; use Encode; use URI::Escape; #从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 ),"n"; # 从unicode得到gb2312编码 $str = '%u6536';
use utf8;  
use Encode;  
use URI::Escape;  
   
   
#从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 ),"n";  
   
# 从unicode得到gb2312编码  
$str = '%u6536';  
$str =~ s/%u([0-9a-fA-F]{4})/pack("U",hex($1))/eg;  
$str = encode( "gb2312","n";  
   
# 从中文得到utf8编码
my $str = '中文';  
print uri_escape_utf8($str),"n";
 
 
# 从utf8编码得到中文  
my $utf8_str = uri_escape_utf8 ("中文");  
print uri_unescape($utf8_str),"n";  
 
# 从中文得到perl unicode  
utf8::decode($str);  
@chars = split //,$str;  
foreach (@chars) {  
    printf "%x ",ord($_),"n";  
}
 
 
# 从中文得到标准unicode
$str = '中文';
map { print "u",sprintf( "%x",$_ ),"n"} unpack( "U*",$str );  
   
 
# 从标准unicode得到中文  
$str = '%u4e2d%u6587';  
$str =~ s/%u([0-9a-fA-F]{4})/pack("U",$str );  
print $str,"n";  
   
# 从perl unicode得到中文  
my $unicode = "x{4e2d}x{6587}";  
print encode( "utf8",$unicode );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读