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 | |
by Eily (Monsignor) on May 17, 2017 at 09:57 UTC |