Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Reading binary file in perl having records of different length

by jaypal (Beadle)
on Jun 17, 2014 at 15:40 UTC ( #1090161=note: print w/ replies, xml ) Need Help??


in reply to Re: Reading binary file in perl having records of different length
in thread Reading binary file in perl having records of different length

Thanks andal. All great suggestions. The binary length suggestion didn't seem to work in my case. I want to capture highest nibble first.

For example, the binary length in hex is 0x03 0x50 so I need to read next 848 bytes. However if I do unpack('s', $buffer) it returns 20483 bytes (reading the binary data as 0x50 0x03).


Comment on Re^2: Reading binary file in perl having records of different length
Re^3: Reading binary file in perl having records of different length
by Anonymous Monk on Jun 17, 2014 at 16:13 UTC

    See pack patterns "n" and "v" instead, or the "<" and ">" modifiers if you really want signed interpretation.

    $ perl -le 'print unpack("v","\x03\x50")' 20483 $ perl -le 'print unpack("n","\x03\x50")' 848 $ perl -le 'print unpack("s<","\x03\x50")' 20483 $ perl -le 'print unpack("s>","\x03\x50")' 848

      Awesome!! The use of "n" pattern worked great.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-09-23 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (222 votes), past polls