|laziness, impatience, and hubris|
converting artibrary binary data to numeric valuesby markle (Initiate)
|on Sep 30, 2012 at 18:00 UTC||Need Help??|
markle has asked for the
wisdom of the Perl Monks concerning the following question:
Give me all the abstract database tasks you like, but I feel like I should wear a dunce cap when it comes to binary data.
I'm getting an HDLC-encoded stream from a device. I can decode it okay. Each packet has a format which contains header information followed by binary-packed payloads of various types and sub-types. These are defined from the device's C/C++ code.
I unpack the bytes using the 'a' template for arbitrary binary data. I can view the data in hex and it corresponds to the byte order of the C struct. However, I don't know what to do to convert some of those binary values to numbers like uint32 or int32 values.
For example, one of the uint32 fields in the C struct is a binary flag. The hex value is 0000 0001. Perl says the value is a string, "\0\0\0\x01". How do I convert this to a numeric value without resorting to unpacking each byte and bit-shifting?
Dunce cap. :-) -Mark