I have a byte array, which originally was converted from a float array in Scala. I need to convert it back to a float array in Python.
This is the code I used to convert the float array in Scala:
val float_ary_len = float_ary.size
val bb = java.nio.ByteBuffer.allocate(float_ary_len * 4)
for(each_float <- float_ary){
bb.putFloat(each_folat)
}
val bytes_ary = bb.array()
Then in Python, I can get this byte array and I need to convert it back to a float array.
I have tried the following code in Python, but it didn't give me the right float.
print(list(bytes_ary[0:4]))
#['\xc2', '\xda', 't', 'Z']
struct.unpack('f', bytes_ary[0:4])
# it gave me 1.7230105268977664e+16, but it should be -109.22725
Please let me know how should I get the right float?
Apparently the Scala code that encodes the value uses a different byte order than the Python code that decodes it.
Make sure you use the same byte order (endianness) in both programs.
In Python, you can change the byte order used to decode the value by using >f
or <f
instead of f
. See https://docs.python.org/3/library/struct.html#struct-alignment.
>>> b = b'\xc2\xdatZ'
>>> struct.unpack('f', b) # native byte order (little-endian on my machine)
(1.7230105268977664e+16,)
>>> struct.unpack('>f', b) # big-endian
(-109.22724914550781,)