unpack "B*", 15

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

why doesn't unpack "B*", 15 print "1111"?
Why do you have to insert pack "N" inbetween?

# perl -e 'print unpack "B*", 15'

# perl -e 'print unpack "B*", pack "N", 15'

Is this some Perl-internal presentation
(some Perl flags, like value=string/numeric?)
which is being printed by the 1st statement?

Thank you

# perl -v
This is perl, v5.8.8 built for i386-linux-thread-multi
# cat /etc/*release
CentOS release 5.2 (Final)

Re: unpack "B*", 15

Quoted text here. Click to load it

unpack's parameters are ALL STRINGS. so that code is equivalent to

 unpack "B*","15"

Quoted text here. Click to load it

pack "N",15 converts the perl number 15 to the binary string containing
a native integer with the value 15, which is what unpack "B*" expects.

Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl /

Re: unpack "B*", 15

Quoted text here. Click to load it

Its doing what you told it to:
"1" is 00110001 and "5" is 00110101

perldoc -f unpack

"takes a string and expands it out into a list of values"

Re: unpack "B*", 15

On Fri, 14 Nov 2008 04:48:16 -0800 (PST),
Quoted text here. Click to load it

You seem to be misunderstanding what unpack and pack do, precisely.

pack takes a list of perl values, and packs them into a 'string', which
probably would better be called a byte array, or byte string, or
something like that. pack and unpack were originally meant to allow the
packing of structs from a perl program in a simiar way a C program would
do it. The resulting 'string' is a binary pattern, resembling the way
data from a C program would live in memory or in binary data files.

unpack takes one of those packed strings, and expands it to a list of
values, according to the template. In this case it would interpret the
string '15' as a bit pattern, and it will expand that to the string
representation of that bit pattern, as per the B field.

Quoted text here. Click to load it

because '15' is not a bit pattern for the number 15. pack "N", 15 is.
'15' is the Perl string consisting of the characters '1' and '5'. you
didn't put quotes there, but unpack will still interpret that argument
as a string, because, well, because it does. That's its job.

pack "N", 15 takes the perl value 15, treats it as a number, and returns
a string whose bytes are the network byte order unsigned long
representation of 15, so you get 4 bytes, the first three of which are
0, and the last is 15, or 0x0f or 0b00001111.

unpack "B*", ...

takes those bytes, and interprets the individual bits in them,
returning the string 00000000000000000000000000001111: 8 zeroes each for
the first three bytes, and 4 zeroes and 4 ones for the last one.

Try the following:

# perl -l
print unpack "B*", pack "C", 15;
print unpack "B*", pack "n", 15;
print unpack "B*", pack "N", 15;
print unpack "B*", pack "v", 15;
print unpack "B*", pack "V", 15;

Here you can see clearly that the unpack is not showing you the bit
representation of 15, but of the packed string. There are many possible
bit representations for the number 15. These are only five of them.

The Vax byte order is little endian, as opposed to the network byte
order, which is big endian, which is what you're looking for.

Quoted text here. Click to load it

This is two bytes with values:

00110001 00110101
49       53

And whose happen to be the ASCII values for the characters 1 and 5,
which is exactly what you put in.

Martien Verbruggen      | Useful Statistic: 75% of the people make up
                        | 3/4 of the population.

Re: unpack "B*", 15

Quoted text here. Click to load it

Others have already explained that 15 is treated as two ASCII characters
'1' and '5'.  I just wanted to point out that in this case you can use chr
instead of pack "N".

perl -e 'print unpack "B*", chr(15)'


-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Re: unpack "B*", 15

On 14 Nov 2008 17:22:32 GMT,
Quoted text here. Click to load it

ITYM instead of pack "C", which also results in one byte. pack "N" would
result in 4 bytes.

Actually.. Since Perl does Unicode, it's probably not entirely that
straightforwardly equivalent. But for numbers small enough (<256), it

Martien Verbruggen      | The problem with sharks is that they are too
                        | large to get to the shallow end of the gene
                        | pool. -- Scott R. Godin

Site Timeline