在线时间:8:00-16:00
132-9538-2358
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Perl中文/unicode/utf8/GB2312编码之间的转换是一件很让人头疼的事情,我总结了以下主要的转换方法,供大家了解参考。# author: jiangyujie 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 );
评论
请发表评论