Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:
Oh great and wise ones I pray for enlightenment. I shall let the code speak first.
#!/usr/bin/perl use warnings; use strict; for(0..32) { my $number = int rand(16**6); my $packed_away = pack "I*" , $number; my @unpacks; push @unpacks, unpack "B32", $packed +_away; push @unpacks, join " ", unpack "B16 B16", $packed +_away; push @unpacks, join " ", unpack "B8 B8 B8 B8", $packed +_away; push @unpacks, join " ", unpack "B4 B4 B4 B4 B4 B4 B4 B4", $packed +_away; printf "%-8i %s\n", $number, (join " ", @unpacks); } __END__ 11689403 00000000101100100101110110111011 0000000010110010 01011101101 +11011 00000000 10110010 01011101 10111011 0000 1011 0101 1011 8041308 00000000011110101011001101011100 0000000001111010 10110011010 +11100 00000000 01111010 10110011 01011100 0000 0111 1011 0101 5462194 00000000010100110101100010110010 0000000001010011 01011000101 +10010 00000000 01010011 01011000 10110010 0000 0101 0101 1011 1901913 00000000000111010000010101011001 0000000000011101 00000101010 +11001 00000000 00011101 00000101 01011001 0000 0001 0000 0101 10562565 00000000101000010010110000000101 0000000010100001 00101100000 +00101 00000000 10100001 00101100 00000101 0000 1010 0010 0000 4113795 00000000001111101100010110000011 0000000000111110 11000101100 +00011 00000000 00111110 11000101 10000011 0000 0011 1100 1000 11898830 00000000101101011000111111001110 0000000010110101 10001111110 +01110 00000000 10110101 10001111 11001110 0000 1011 1000 1100 5570479 00000000010101001111111110101111 0000000001010100 11111111101 +01111 00000000 01010100 11111111 10101111 0000 0101 1111 1010
Sorry for the ugly output, it does not line wrap on my tty here.
Why do I get 32 bits of binary for all the unpacks except the last one (eight times 4 bits) ? The last one is of course the one I want for the script I am playing with :(
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: binary pack/unpack oddness
by BrowserUk (Patriarch) on Jan 24, 2005 at 18:36 UTC | |
by Random_Walk (Prior) on Jan 24, 2005 at 18:46 UTC |