Reputation: 700
I am sort of a novice to the Python language and am having a hard time doing something I could very easily do with C++ or Java but for some reason seems so convoluted to do in Python. I have the following four bytes in an array (in big endian order):
[0x64, 0xD8, 0x6E, 0x3F]
I already know beforehand what these bytes represent. They specify the following 32-bit floating-point number: 0.932989
What are the steps I need to perform using Python (preferably v3.2.1 and without using extra imports) to interpret those 4 bytes as that float and store that number in a variable which I can manipulate as a 32-bit floating-point value? I.e. so i can use it just as the following variable myVar = 0.932989
I've tried:
x = [0x64, 0xd8, 0x6e, 0x3f]
y = int.from_bytes(x, byteorder='little', signed=False) #interpret bytes as an unsigned little-endian integer (so far so good)
z = float(y) #attempt to cast as float reinterprets integer value rather than its byte values
y
has the right expected integer interpretation of those bytes, which is 1064228964
, the problem comes when casting that to a 32-bit float
. Instead of casting the raw bytes of y
as a float, it casts the integer representation of those bytes, so z
contains 1064228964.0
instead of the desired value of 0.932989
. Is there maybe something equivalent to int.from_bytes
that I can use to perform this simple task? Perhaps something like float.from_bytes
?
Upvotes: 19
Views: 43284
Reputation: 7941
For detail see Python Struct. For your specific question:
import struct
# if input is string, per @robyschek will fail on python 3
data=b'\x64\xd8\x64\x3f'
print struct.unpack('<f', data) #little endian
print struct.unpack('>f', data) # big endian
#your input
list1=[0x64, 0xD8, 0x6E, 0x3F]
# aa=str(bytearray(list1)) # edit: this conversion wasn't needed
aa= bytearray(list1)
print struct.unpack('<f', aa)
output:
(0.8939268589019775,)
(3.193376169798871e+22,)
(0.9329893589019775,)
Upvotes: 23
Reputation: 3254
If you're willing to use a big library that's really for handling (large) arrays of numbers efficiently:
import numpy as np
data_bytes = np.array([0x64, 0xD8, 0x6E, 0x3F], dtype=np.uint8)
data_as_float = data_bytes.view(dtype=np.float32)
print(data_as_float)
This will also work on big byte arrays; then you get an array of floats.
Upvotes: 9