Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

further subdivision of unpack result

by jjap (Monk)
on Apr 13, 2011 at 02:54 UTC ( #899090=perlquestion: print w/ replies, xml ) Need Help??
jjap has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

This is my first try at handling a binary file. I have successfully slurped a long string of bits into $trailing:

 ($id, $trailing) = unpack ("A16 x72 B*", $data);

I now need to divide that string into 8 bits x 4 repeatedly over the string, which may vary from file to file but will always be a multiple of 32. I was wondering how to go about this...

 (byte2, byte1, byte4, byte3) = ?????

It is likely trivial but I am stumped!
Thanks for any hints...

Update Sorry for bothering you, a loop with substr should do it...  substr $trailing, 0, 8, "" ;

Update2 To answer the first question of BrowserUK I read the bits because that filled my variable, it did not occur to me to capture the result in an array !! Many thanks to all!
---jjap

Comment on further subdivision of unpack result
Select or Download Code
Re: further subdivision of unpack result
by BrowserUk (Pope) on Apr 13, 2011 at 03:53 UTC

    Why are you converting to bits only then to have to convert back to bytes?

    Reading between the lines of your post, you could use this to get at the bytes:

    ($id, @bytes) = unpack ("A16 x72 C*", $data);

    and then just access the them via the array.

    But then, looking at the way you are numbering your 4 bytes: (byte2, byte1, byte4, byte3) it seems likely that each group of 4 bytes is actually a little endian 32-bit value. In which case, you can skip the bytes step also and do:

    ($id, @dwords) = unpack ("A16 x72 (L<)*", $data);

    It's a lot simpler to use the built-in facilities :)


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Little-endian isn't 2,1,4,3, it's 4,3,2,1.
      >perl -E"say for unpack 'H*', pack 'L<', 0x11223344" 44332211
Re: further subdivision of unpack result
by GrandFather (Cardinal) on Apr 13, 2011 at 03:53 UTC

    I'd do it something like this:

    #!/usr/bin/perl -w use warnings; use strict; my $data = <<DATA; 16 character ID \x00\x01\x02\x03\x10\x11\x12\x13 DATA my $id = unpack ("A16", substr $data, 0, 16, ''); print "$id\n"; while (length $data) { my (@bytes) = unpack ('c4', substr $data, 0, 4, ''); printf "0x%02x ", $_ for @bytes; print "\n"; }

    Prints:

    16 character ID 0x00 0x01 0x02 0x03 0x10 0x11 0x12 0x13 0x0a
    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-12-25 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls