Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Unable to unpack a hex string

by Eily (Prior)
on May 17, 2017 at 08:18 UTC ( #1190447=note: print w/replies, xml ) Need Help??


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 (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190447]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-11-17 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (204 votes). Check out past polls.

    Notices?