perl 对二进制文件的操作
时间:2010-08-09 来源:rrgela
sub ChangeByte
{
my $file=shift();
my $Byte=shift();
my $value=shift();
open(FH,"+<$file") or die "File not exist \n";
binmode FH;
seek (FH,$Byte,0);
print (FH pack("H*",$value));
close(FH);
}
sub ChangeBit { my $file=shift(); my $Byte=shift(); my $Bit=shift(); #my $value1=shift();
open(FH,"+<$file") or die "File not exist \n"; binmode FH; my $charb; seek (FH,$Byte,0); read(FH,$charb,1); my $Bit2; $Bit2=7-$Bit; ($charb)=unpack("B*",$charb); #print "the Byte is $charb"; my $chara=substr($charb,0,$Bit2); my $value=substr($charb,$Bit2,1); my $value1; if ($value eq "1") { $value1="0"; } else { $value1="1"; } my $charc=substr($charb,$Bit2+1,$Bit); $charb=$chara.$value1.$charc; seek (FH,$Byte,0); print (FH pack("B*",$charb)); close(FH); }
sub ChangeBit { my $file=shift(); my $Byte=shift(); my $Bit=shift(); #my $value1=shift();
open(FH,"+<$file") or die "File not exist \n"; binmode FH; my $charb; seek (FH,$Byte,0); read(FH,$charb,1); my $Bit2; $Bit2=7-$Bit; ($charb)=unpack("B*",$charb); #print "the Byte is $charb"; my $chara=substr($charb,0,$Bit2); my $value=substr($charb,$Bit2,1); my $value1; if ($value eq "1") { $value1="0"; } else { $value1="1"; } my $charc=substr($charb,$Bit2+1,$Bit); $charb=$chara.$value1.$charc; seek (FH,$Byte,0); print (FH pack("B*",$charb)); close(FH); }
相关阅读 更多 +