in reply to Pack/unpack irregularity

It doesn't make much sense to me either. I can suggest a workaround, however, and leave the technical explanation to someone who understands it better than I do.

Instead of
sprintf "%1x", $var;
use
sprintf "%2x", $var;

This will give two nybbles (a full byte) so pack and unpack give the expected answer.

Update: Changed bytes to nybbles - Doh!

Update 2: ++ greenFox for pointing out the why.

Replies are listed 'Best First'.
Re: Re: Pack/unpack irregularity
by greenFox (Vicar) on May 30, 2002 at 01:17 UTC

    from the pack man page- "With all types except "a", "A", "b", "B", "h", "H", and "P" the pack function will gobble up that many values from the LIST, <snip>The "h" and "H" fields pack a string that many nybbles long."

    --
    my $chainsaw = 'Perl';

      Good call! Now, if I can get the darned thing to right justify, I'm set, 'cuz a leading zero should cause no problems at all (crosses fingers and hopes that that is true...)

      thanks,thor