SPUG: pack and unpack

Ben Reser ben at reser.org
Wed Aug 27 14:22:34 CDT 2003


On Wed, Aug 27, 2003 at 11:47:39AM -0700, Richard Wood wrote:
> ($tagB, $typeB, $countB, $val1B, $val2B, $val3B,
> $val4B) = unpack "B16B16B32B8B8B8B8", $buffer;
> $tagC = bin2dec($tagB);
> $typeC = bin2dec($typeB);
> $countC = bin2dec($countB);
> 
> sub bin2dec {
>     return unpack("N", pack("B32", substr("0" x 32 .
> shift, -32)));
> }

Why not do this:


seek(TIF, $offset, 0) or die "Seek $i: $!\n";
read(TIF, $buffer, 12);
($tag, $type, $count, $value) = unpack "nnNB32", $buffer;
if ($type == 1) {
  $decoded_value = unpack "C", $value;
} elsif ($type == 2) {
  $decoded_value = unpack "Z$count", $value;
} elsif ($type == 3) {
  $decoded_value =unpack "n", $value;
} elsif ($type == 4) {
  $decoded_value = unpack "N", $value;
...

-- 
Ben Reser <ben at reser.org>
http://ben.reser.org

"What upsets me is not that you lied to me, but that from now on I can
no longer believe you." -- Nietzsche



More information about the spug-list mailing list