Convert Bytes to Floating Point Numbers in Python *

Question

I have a binary file that I have to parse and I'm using Python. Is there a way to take 4 bytes and convert it to a single precision floating point number?

Answer

>>> import struct
>>> struct.pack('f', 3.141592654)
'\xdb\x0fI@'
>>> struct.unpack('f', '\xdb\x0fI@')
(3.1415927410125732,)
>>> struct.pack('4f', 1.0, 2.0, 3.0, 4.0)
'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/5415/" >Convert Bytes to Floating Point Numbers in Python< /a>
Share on Google Plus

About Cinema Guy

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment