Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Unable to unpack a hex string

by kjoy10970 (Initiate)
on May 17, 2017 at 05:49 UTC ( [id://1190430]=perlquestion: print w/replies, xml ) Need Help??

kjoy10970 has asked for the wisdom of the Perl Monks concerning the following question:

With the below code , it only unpacks the first 4 values and the rest of the values are coming null. I need to get all the values, what am I doing wrong here? Please help me. Code is down here

use Data::Dumper; my $counter = 'a0000000'; my @x= unpack('(B3)(B3)(B2)(B)(B)(B)(B2)(B3)BBBBB(B11)', pack('H*', $c +ounter)); print Dumper(\@x);

Output:

$VAR1 = [ '101', '000', '00', '0', '', '', '', '', '', '', '', '', '', '' ];

Replies are listed 'Best First'.
Re: Unable to unpack a hex string
by Eily (Monsignor) on May 17, 2017 at 08:18 UTC

    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.

      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.

Re: Unable to unpack a hex string
by tybalt89 (Monsignor) on May 17, 2017 at 06:24 UTC
    my @x = unpack('(a3)(a3)(a2)(a)(a)(a)(a2)(a3)aaaaa(a11)', unpack 'B*', + pack('H*', 'a0000001'));
Re: Unable to unpack a hex string
by AnomalousMonk (Archbishop) on May 17, 2017 at 14:03 UTC

    Your use of parentheses around some of the Bs in the OPed code and not others suggests that you might want to skip over the BBBBB group. If so:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $counter = '87654321'; my @x = unpack 'a3 a3 a2 a a a a2 a3 x[a5] a11', unpack 'B*', pack 'H*', $counter ; dd \@x; " [100, "001", 11, 0, 1, 1, "00", 101, "01100100001"]


    Give a man a fish:  <%-{-{-{-<

      Thank a lot , I understood the mistake and could correct it

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1190430]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found