Re: data become zero after shift



I modified my script.But I found that the unpacked value can't be
transmit to sub routine.

======
use strict;

my $string="不正确";
my $offset=0;
my $substring="";

if ($string=~/^((\C)(\C)(\C))/) {print $1."\n";}

my $firstbyte;
my $secondbyte;
my $thirdbyte;

my $first_unpack;
my $second_unpack;
my $third_unpack;

my
@table=("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","+","/","=");

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){last;}

if ($firstbyte eq undef)
{
last;
}else
{
my $first_unpack=unpack("C",$firstbyte);
print "\nfirst unpack is: ";
print $first_unpack;
print "\n";
}

if ($secondbyte eq undef)
{
$second_unpack=0;
}else
{
my $second_unpack=unpack("C",$secondbyte);
}

if ($thirdbyte eq undef)
{
$third_unpack=0;
}else
{
my $third_unpack=unpack("C",$thirdbyte);
}

print pack3($first_unpack,$second_unpack,$third_unpack);
#print "\nloop\n";

if ($secondbyte eq undef){last;}
if ($thirdbyte eq undef){last;}

}

sub pack3
{
my
@table=("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","+","/","=");

my $first_unpack2=$_[0];
my $first_unpack=$first_unpack;
print "\nfirst incomming unpack is: ";
print $first_unpack; #it become 0
print "\n";
my $second_unpack=$_[1];
my $third_unpack=$_[2];

my $firstchar=(($first_unpack)>>2);
print "\nfirstchar is: ";
print $firstchar;
print "\n";
my
$secondchar=(($first_unpack<<4)&0b00110000)|($second_unpack>>4);
my $thirdchar=(($second_unpack<<2)&0b00111100)|($third_unpack>>6);
my $forthchar=$third_unpack&0b00111111;
return
$table[$firstchar].$table[$secondchar].$table[$thirdchar].$table[$forthchar];
}
======

.



Relevant Pages

  • shift error
    ... The program extract data from a UTF-8 string successfully,but it become ... if ($secondbyte eq undef) ... if ($thirdbyte eq undef) ... if ($secondbyte eq undef) ...
    (perl.beginners)
  • data become zero after shift
    ... The program extract data from a UTF-8 string successfully,but it become ... if ($secondbyte eq undef) ... if ($thirdbyte eq undef) ... if ($secondbyte eq undef) ...
    (perl.beginners)