thor has asked for the wisdom of the Perl Monks concerning the following question:
...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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jeffa) Re: Pack/unpack irregularity
by jeffa (Bishop) on May 30, 2002 at 04:38 UTC | |
by greenFox (Vicar) on May 30, 2002 at 23:51 UTC | |
|
Re: Pack/unpack irregularity
by jsprat (Curate) on May 30, 2002 at 00:47 UTC | |
by greenFox (Vicar) on May 30, 2002 at 01:17 UTC | |
by thor (Priest) on May 30, 2002 at 03:40 UTC | |
|
Re: Pack/unpack irregularity
by rbc (Curate) on May 29, 2002 at 23:58 UTC | |
by thor (Priest) on May 30, 2002 at 00:15 UTC |