Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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!

Replies are listed 'Best First'.
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 (Sage) 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"; }


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2016-08-28 20:37 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (395 votes). Check out past polls.