unpack "B*", 15

A

A. Farber

Hello,

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

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

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

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

Thank you
Alex

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

Joost Diepenmaat

A. Farber said:
Hello,

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

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

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

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

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.
 
S

smallpond

Hello,

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

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

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"
 
M

Martien Verbruggen

Hello,

why doesn't unpack "B*", 15 print "1111"?

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.
Why do you have to insert pack "N" inbetween?

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;
__END__

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.
# perl -e 'print unpack "B*", 15'
0011000100110101

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
 
X

xhoster

A. Farber said:
Hello,

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

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

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

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)'
00001111

Xho

--
-------------------- 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.
 
M

Martien Verbruggen

Hello,

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

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

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

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".[/QUOTE]

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
is.

Martien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top