in reply to Problems using pack

If you pack a string into a variable and then unpack it, it must be equal, and that has nothing to do with writing to a file. Try this:
$g = "0110110001101101"; $bob = pack("B*", $g); print $g . "\n"; print unpack("B*", $bob);
You should get the same results. Now, try a file (remove the -l option of perl, as it inserts a new line):
open(FH, ">fh"); $g = "0110110001101101"; $bob = pack("B*", $g); print FH $bob; close(FH); open(FH, "fh"); read(FH, $from_file, 2); print $g . "\n"; print unpack("B*", $from_file), "\n";
And report the results (note, I changed the bytes to be visible, now it should print "lm")

Replies are listed 'Best First'.
Re: Re: Problems using pack
by Anonymous Monk on Aug 19, 2002 at 07:04 UTC
    You are correct, they must be equal. I tried your code and it worked wonderfully. The error in what I did is that the long decimal numbers I was using must be in quotes otherwise, as tye said, they would be interrpeted as 1.101100011e12 which is not what I wanted. Sticking it in a variable such as $g and then using
    $bob = pack("B*",$g);
    does the trick. Thanks everyone for helping with this!