|
| This is a part of a program which used to do base64 encode.
The program extract data from a UTF-8 string successfully,but it become
zero after shift operation.
The code is shown below
=3D=3D=3D=3D=3D=3D=3D
use strict;
#my $string=3D"http://www.flickr.com/photos/cyberblue";
#my $string=3D"ttp=E4=B8=8D=E6=AD=A3=E7=A1=AE";
my $string=3D"=E4=B8=8D=E6=AD=A3";
my $offset=3D0;
my $substring=3D"";
my $firstbyte;
my $secondbyte;
my $thirdbyte;
my $firstbyte_length;
my $secondbyte_length;
my $thirdbyte_length;
my
@table=3D("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","=
Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i",=
"j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1"=
,"2","3","4","5","6","7","8","9","+","/","=3D");
pipe(my $out,my $in);
print $in $string;
close $in;
binmode $out;
while(1)
{
read($out,$firstbyte,1);
read($out,$secondbyte,1);
read($out,$thirdbyte,1);
if ($firstbyte eq undef){print "last!";last;}
#print $firstbyte." 1 ";
#print $secondbyte." 2 ";
#print $firstbyte." 3 ";
print pack3($firstbyte,$secondbyte,$thirdbyte)
;
#print "\nloop\n";
if ($secondbyte eq undef){last;}
if ($thirdbyte eq undef){last;}
}
sub pack3
{
my
@table=3D("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","=
Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i",=
"j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1"=
,"2","3","4","5","6","7","8","9","+","/","=3D");
my $firstbyte=3D$_[0];
my $secondbyte=3D$_[1];
my $thirdbyte=3D$_[2];
my $first_unpack;
my $second_unpack;
my $third_unpack;
if ($firstbyte eq undef)
{
return "";
}else
{
my $first_unpack=3Dunpack("C",$firstbyte);
print "\nfirst unpack is: ";
print $first_unpack; # I get correct data here
print "\n";
}
if ($secondbyte eq undef)
{
$second_unpack=3D0;
}else
{
my $second_unpack=3Dunpack("C",$secondbyte);
}
if ($thirdbyte eq undef)
{
$third_unpack=3D0;
}else
{
my $third_unpack=3Dunpack("C",$thirdbyte);
}
my $firstchar=3D(($first_unpack)>>2);
print "\nfirstchar is: ";
print $firstchar; # but it become zero here
print "\n";
my
$secondchar=3D(($first_unpack<<4)&0b00110000)|($second_unpack>>4);
my $thirdchar=3D(($second_unpack<<2)&0b00111100)|($third_unpack>>6);
my $forthchar=3D$third_unpack&0b00111111;
return
$table[$firstchar].$table[$secondchar].$table[$thirdchar].$table[$forthchar=
];
}
=3D=3D=3D=3D=3D=3D
thanks.
|
|