...and now the output:#!/usr/local/perl5.006/bin/perl -w use strict; my $var = shift; print "var: $var\n"; my $temp = sprintf "%lx", $var; print "temp: $temp\n"; my $packed = pack "H*", $temp; print "packed: $packed\n"; $temp = unpack "H*", $packed; print "temp: $temp\n"; my $unpacked = hex $temp; print "unpacked: $unpacked\n";
I don't understand why when I unpack the variable $packed, it doesn't come back with what I packed it with. What I also find strange is that the behavior alternates depending on what range the input is in. Here's what I've found (x represents the input value):>pack.pl 12 var: 12 temp: c packed: À temp: c0 unpacked: 192 >
Definately a pattern. The way I figure it, pack is packing this in to the appropriate number of bytes, and when it doesn't have enough to fill an entire byte, it 0 pads to the right. Don't know how to exploit that, however (other than a hack with logarithms to find out what range it's in and act accordingly)16^0 <= x < 16^1 (# of hex digits is odd) bad 16^1 <= x < 16^2 (# of hex digits is even) good 16^2 <= x < 16^3 (# of hex digits is odd) bad 16^3 <= x < 16^4 (# of hex digits is even) good ...
any help would be appreciated,
thor
In reply to Pack/unpack irregularity by thor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |