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

Re^2: Unpacking variable length bit fields

by puterboy (Scribe)
on Jun 19, 2012 at 04:15 UTC ( #976953=note: print w/ replies, xml ) Need Help??

in reply to Re: Unpacking variable length bit fields
in thread Unpacking variable length bit fields

Oops, I spoke to soon. The field length specifies the number of bits (multiple of 8) but I want to extract the fields themselves as bytes not as bit strings of 1/0's.

My problems is that using something like: unpack('C/C'...) would unpack C bytes for each filed rather than C bits for each field which is what I want. So I really would like to divide by 8 but I'm not sure how to do that. I imagine I could unpack as a bit string of 1/0's then split, then repack but that seems klugey.

It's a screwy format, but I don't have control over it....

Comment on Re^2: Unpacking variable length bit fields
Re^3: Unpacking variable length bit fields
by BrowserUk (Pope) on Jun 19, 2012 at 05:30 UTC

    The the easiest way is to convert the ascii-ised binary back to real numbers:

    $bitstream = "\x18\xf0\xff\x0f\x10\xaa\x55\x20\xde\xad\xbe\xef";; ( $f1, $f2, $f3 ) = map unpack( 'L', pack( 'b32', $_ )), unpack 'C/b C +/b C/b', $bitstream;; print for $f1, $f2, $f3;; 1048560 21930 4022250974

    This assumes maximum 32-bit values. You could use Q if you are on a 64-bit platform and Perl and need that.

    By converting them all to the maximum sized integer the platform can handle you save having to make decisions and they'd end up that way anyway.

    You might need to adjust the L (or the 'b32' to 'B32' or 'B64') for the endianness of the originating platform.

    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2014-09-17 03:41 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (57 votes), past polls