linux 把二进制翻译成中文/英文
01
#!/usr/bin/perl -w
02
#use diagnostics;
03
sub bin2cn{
04
die "Error:1001:bin2cn\n" if chop($_[0]) eq "";
05
@hexi = &bin2hex($_[0]);
06
while(@hexi){
07
$q=shift @hexi if @hexi;#noting
08
$temp=&hex2int($q);
09
$q=shift @hexi if @hexi;
10
$temp=$temp*16+&hex2int($q);
11
push @cni, chr($temp);
12
}
13
join '', @cni
14
}
15
16
sub bin2hex{
17
my @binarr=('0000','0001','0010','0011','0100','0101','0110','0111','1000','1001','1010','1011','1100','1101','1110','1111');
18
my @hexarr=("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
19
20
if(length($_[0]) % 4 ){
21
my @in = &split4($_[0]);
22
while(my $ta=shift @in){
23
for (0..15){
24
push @re, $hexarr[$_] if ($binarr[$_] eq $ta) ;
25
}
26
}
27
@re
28
}else{
29
print "Error:1001:bin2hex\n";
30
exit
31
}
32
}
33
sub hex2bin{
34
my @binarr=('0000','0001','0010','0011','0100','0101','0110','0111','1000','1001','1010','1011','1100','1101','1110','1111');
35
my @hexarr=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
36
my @in = split //, $_[0];
37
while(@in){
38
my $tai=shift @in;#noting
39
for (0..15){
40
push @reo, $binarr[$_] if ($hexarr[$_] eq $tai);
41
}
42
}
43
join '', @reo
44
}
45
sub split4{
46
my $ini = $_[0];
47
push @res, substr($ini,$_,4) for (grep {$_ % 4 == 0 } 0..length($ini));
48
@res
49
}
50
51
sub hex2int{
52
if ($_[0] =~ /^[ABCDEF]$/){
53
ord($_[0])-65+10;
54
}elsif($_[0] =~ /^[0123456789]$/){
55
ord($_[0])-48;
56
}else{
57
print "\nError: 1001:hex2int\n";
58
exit
59
}
60
}
61
#open IN, "<./binaryfile";
62
#my $tee= <IN>;
63
#my @sd=&split4($tee);
64
#print "SS: @sd\n";
65
#print "OO: ".(length($tee) % 8)."\n"; 0
66
67
#print ord(1)." FF: ".&hex2int(1)."\n";
68
69
#my $Stee=&bin2cn($tee);
70
#print "TT: $Stee\n";
71
72
my $ods= "E4B896E7958CEFBC8CE4BDA0E5A5BDE38082";
73
my $des=&hex2bin($ods);
74
my $Stea=&bin2cn($des);
75
print "TT: $Stea\n";
转自 开源中国 Jahdo_Lee 发布于 2013年08月12日 22时