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