in reply to Unable to unpack a hex string

Actually only the first value is correct. That's because pack and unpack work on byte aligned positions, so the second set of three bits is read at position 8 rather than 3. This is visible if your input data contains something else than zeros:

use Data::Dumper; my $counter = 'a0b0c0d0'; my @x= unpack('(B3)(B3)(B2)(B)(B)(B)(B2)(B3)BBBBB(B11)', pack('H*', $c +ounter)); print Dumper(\@x); __DATA__ $VAR1 = [ '101', '101', '11', '1', '', '', '', '', '', '', '', '', '', '' ];

FYI, another way to read bits from a string is to use vec, that makes it possible to read N bits from position P.

Replies are listed 'Best First'.
Re^2: Unable to unpack a hex string
by BrowserUk (Patriarch) on May 17, 2017 at 09:44 UTC
    FYI, another way to read bits from a string is to use vec, that makes it possible to read N bits from position P.

    Yes, but it also has byte/word/doubleword/quadword alignment restrictions.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

      Oh right, thanks. So that's either pack and unpack or binary masking and shifting operations (maybe combined with vec) on integer values then.