标题: [原创代码] Perl版汉字批量转UTF-8编码[纯编码] [打印本页]
作者: PakTC 时间: 2013-7-29 17:55 标题: Perl版汉字批量转UTF-8编码[纯编码]
本帖最后由 CrLf 于 2015-7-9 01:26 编辑
保存脚本的时候须为utf-8格式。- #!/usr/bin/perl
- #Code by Paktc
- #2010-09
-
- $lv_[1]=00000000;
- $lv_[2]=11000000;
- $lv_[3]=11100000;
- $lv_[4]=11110000;
- $lv_[5]=11111000;
- $lv_[6]=11111100;
-
- use utf8;
-
- $a="千与千寻";
- $L=length($a)-1;
- for $i (0..$L) {
- $b=$i;
- printf "%s %s\n",&code2utf(ord(substr($a,$i,2)));
- }
- print "END!";
- <STDIN>;
-
-
- sub code2utf {
- my ($code,$bin,$level,$hex,$tmp,$result);
- $code=$_[0];
- $bin=sprintf "%b",$code;
-
- $level=1;
- if (length($bin)>7) {
- while (length($bin)>=6) {
- $level+=1;
- $bin=~s/.{6}$//;
- $hex=sprintf "%X",&bin2int(10 . $&);
- $result=$hex ."-". $result;
- }
- }
-
- $tmp=substr($lv_[$level],0,8-length($bin));
- $hex=sprintf "%X",&bin2int($tmp . $bin);
- $result=$hex ."-". $result;
- $result=~s/-$//;
- $result; #result - string
- }
-
- sub bin2int {
- my ($a,$str,$all,$act);
- $a=$_[0];
- $str="";
- $all=0;
- $act=0;
- do {
- $b=($a % 10) * (2**$act); #** as ^ of the calc
- $all+=$b;
- $act+=1;
- $a=int ($a/10);
- } until ($a==0);
- int ($all);
- }
复制代码
输出
E5-8D-83
E4-B8-8E
E5-8D-83
E5-AF-BB
END!
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |