Convert Bytes to Floating Point Numbers in Python
<p>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?</p>
<pre><code>&gt;&gt;&gt; import struct
&gt;&gt;&gt; struct.pack('f', 3.141592654)
b'\xdb\x0fI@'
&gt;&gt;&gt; struct.unpack('f', b'\xdb\x0fI@')
(3.1415927410125732,)
&gt;&gt;&gt; struct.pack('4f', 1.0, 2.0, 3.0, 4.0)
'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'
</code></pre>
