1

I am sending 1.0 and 2.0 as binary from C program to STDOUT:

    float array[2] = { 1.0, 2.0 };
    fwrite(&array[0], sizeof(array[0]), sizeof(array) / sizeof(array[0]), stdout);
    fflush(stdout);

Python program reads STDOUT as:

b'\x00\x00\x80?\x00\x00\x00@'

When I try to convert this back to float, this works:

struct.unpack('<f', array[0:4]) # 1.0
struct.unpack('<f', array[4:8]) # 2.0

However, if I try to decode the whole array at once, it fails:

struct.unpack('<f', array)

with the following message:

error: unpack requires a buffer of 4 bytes

Is it possible to decode the whole array at once, or I should decode in a loop each float separately?

2 Answers 2

5

Using struct

Base on the struct module documentation the struct.unpack() function must have defined exact format for unpacking multiple values at once. So if you need to use struct module, then you either have to define the format using format as '<ff' or iterate over the array using struct.iter_unpack(format, buffer).

Using array

Another option is to use array module, which allows you to decode the binary data at once. You can change endianness using array.byteswap() method. The only restriction is that the whole array must have same type.

import array

arr = array.array('f', b'\x00\x00\x80?\x00\x00\x00@')

arr.tolist()
# [1.0, 2.0]

# change endianness
arr.byteswap()

arr.tolist()
# [4.600602988224807e-41, 8.96831017167883e-44]
Sign up to request clarification or add additional context in comments.

Comments

1

Python's strcut provide some essential tooling, but is a bit clumsy to work with. In particular, for this, it'd have to yield all your floats as a tuple of Python floats (which are "enormous" objetcs with 10s of bytes each), and then insert then into an array.

Fortunatelly, Python's array.array class itself can fill in its values given a bytes object, or even read then directly from a file.


import array

data = b'\x00\x00\x80?\x00\x00\x00@'
arr = array.array('f')
arr.frombytes(data)
print(arr)

Yields:

array('f', [1.0, 2.0])

(to read directly from a file, use the fromfile method)

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.