in reply to binary pack/unpack oddness

The minimum chunk of memory processed by any single unpack pattern element is a byte. Hence, when you unpack a 32-bit element using repeated B4 elements, you get a value that represents the high 4-bits of each byte and the low 4-bits ar discarded. This may be made clearer by:

$b = pack 'I*', 0x12345678; print join'|', unpack '(B4)*', $b; 0111|0101|0011|0001

As you can see the (four!) values returned by unpack are the high 4-bits from each of the 4 bytes (in reverse order).

What I think you are after can be done like this:

$b = pack 'I*', 0x12345678; print join'|', unpack '(A4)*', unpack 'B*', $b; 0111|1000|0101|0110|0011|0100|0001|0010

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re^2: binary pack/unpack oddness
by Random_Walk (Prior) on Jan 24, 2005 at 18:46 UTC

    Ah Ha! that's the problem.

    I had the internal assumption that unpack would just take as many bits as it needed and keep the rest for the next unpack. Your solution is also perfect.

    Many Thanks,
    R.

    Pereant, qui ante nos nostra dixerunt!