I want to convert a Python float into a byte array, encoding it as a 32 bit little-endian IEEE floating point number, in order to write it to a binary file.
What is the modern Pythonic way to do that in Python 3? For ints I can do my_int.to_bytes(4,'little')
, but there is no to_bytes
method for floats.
It's even better if I can do this in one shot for every float in a numpy array (with dtype numpy.float32). But note that I need to get it as a byte array, not just write the array to a file immediately.
There are some similar-sounding questions, but they seem mostly to be about getting the hex digits, not writing to a binary file.