PERL PERL5 PORTERS 12 RE PERL 34168 UNPACK USING A STRING INSTEAD OF A NUMBER FOR H AND B
Date: Thu, 17 Feb 2005 09:13:31 -0500

Subject: Re: [perl #34168] Unpack using a string instead of a number for H and B
From: rjk-perl-p5p@no-spam (Ronald J Kimball)

On Thu, Feb 17, 2005 at 01:17:36PM -0000, Joshua Isom wrote:
> # New Ticket Created by Joshua Isom > # Please include the string: [perl #34168]
> # in the subject line of all future correspondence about this issue. > # <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=34168 >
> > > When using unpack, it treats the argument as a string by default. For > example:

Thank you for your report. However, this is the documented behavior for unpack:

unpack TEMPLATE,EXPR
"unpack" does the reverse of "pack": it takes a string and expands it out into a list of values. (In scalar context,
it returns merely the first value produced.)

> foreach (0 .. 0xF) {
> print +(unpack 'H*', $_), ' ';
> }
> > Returns:
> > 30 31 32 33 34 35 36 37 38 39 3130 3131 3132 3133 3134 3135
> > Although I would expect it to return:
> > 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
I'm not sure why you would want to use unpack for this. I think you want sprintf instead:

foreach (0 .. 0xF) {
print sprintf('%02x', $_), ' ';
}

Ronald