Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Assuming your bitfields are all multiples of 8, then using unpack's length/type nomenclature will work.

$bitstream = "\x18\xf0\xff\x0f\x10\xaa\x55\x20\xde\xad\xbe\xef";; ( $f1, $f2, $f3 ) = unpack 'C/b C/b c/B', $bitstream;; print for $f1, $f2, $f3;; 000011111111111111110000 0101010110101010 11011110101011011011111011101111
You'll need to use the correct variations of 'c' or 'C' (probably the latter) and b/B (which will depend upon the endianess of the originating hardware. I've used a mixture here to demonstrate the affect of some of the combinations.

Note also that the output is ascii-ised binary. That is, each bit of the input is translated to a byte containing ascii '0' or ascii '1'.

Other variations are possible. For example, you might want to retain the bits as binary encoded fields and then manipulate them with vec. But you'd need to explain more about what they are.


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?


In reply to Re: Unpacking variable length bit fields by BrowserUk
in thread Unpacking variable length bit fields by puterboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found