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?

0

3 Answers 3

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,)
Sign up to request clarification or add additional context in comments.

1 Comment

From the Java ByteBuffer documentation: "The initial order of a byte buffer is always BIG_ENDIAN."
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])

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.