How to convert a byte array to float in Python
Asked Answered
P

3

5

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?

Philine answered 31/5, 2019 at 13:59 Comment(0)
F
12

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,)
Fidget answered 31/5, 2019 at 14:24 Comment(1)
From the Java ByteBuffer documentation: "The initial order of a byte buffer is always BIG_ENDIAN."Skep
B
2

It could be because of the endian encoding.

You should try big endian:

struct.unpack('>f', bytes_ary[0:4])

or little endian:

struct.unpack('<f', bytes_ary[0:4])
Bastian answered 31/5, 2019 at 14:22 Comment(0)
M
0

Depends on your byte array.

if print(byte_array_of_old_float) returns bytearray(b'684210')

then this should work: floatvar=float(byte_array_of_old_float)

In my case the byte array came from a MariaDB select call, and I did the conversion like that.

Metatarsal answered 7/2, 2021 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.