Skip to content Skip to sidebar Skip to footer

Converting Python Float To Bytes

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 Pyth

Solution 1:

You could use struct to pack the bytes like,

>>>import struct>>>struct.pack('<f', 3.14) # little-endian
b'\xc3\xf5H@'
>>>struct.pack('>f', 3.14) # big-endian
b'@H\xf5\xc3'

Solution 2:

NumPy arrays come with a tobytes method that gives you a dump of their raw data bytes:

arr.tobytes()

You can specify an order argument to use either C-order (row major) or F-order (column major) for multidimensional arrays.

Since you want to dump the bytes to a file, you may also be interested in the tofile method, which dumps the bytes to a file directly:

arr.tofile(your_file)

tofile always uses C-order.

If you need to change endianness, you can use the byteswap method. (newbyteorder has a more convenient signature, but doesn't change the underlying bytes, so it won't affect tobytes.)

import sys
if sys.byteorder=='big':
    arr = arr.byteswap()
data_bytes = arr.tobytes()

Solution 3:

With the right dtype you can write the array's data buffer to a bytestring or to a binary file:

In [449]: x = np.arange(4., dtype='<f4')                                        
In [450]: x                                                                     
Out[450]: array([0., 1., 2., 3.], dtype=float32)
In [451]: txt = x.tostring()                                                    
In [452]: txt                                                                   
Out[452]: b'\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00@\x00\x00@@'
In [453]: x.tofile('test')                                                                                                                           
In [455]: np.fromfile('test','<f4')                                             
Out[455]: array([0., 1., 2., 3.], dtype=float32)
In [459]: with open('test','br') as f: print(f.read())                          
b'\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00@\x00\x00@@'

Change endedness:

In [460]: x.astype('>f4').tostring()                                            
Out[460]: b'\x00\x00\x00\x00?\x80\x00\x00@\x00\x00\x00@@\x00\x00'

Solution 4:

There are save/savez methods in numpy:

Store data to disk, and load it again:

>>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]]))
>>> np.load('/tmp/123.npy')
array([[1, 2, 3],
       [4, 5, 6]])

Store compressed data to disk, and load it again:

>>>a=np.array([[1, 2, 3], [4, 5, 6]])>>>b=np.array([1, 2])>>>np.savez('/tmp/123.npz', a=a, b=b)>>>data = np.load('/tmp/123.npz')>>>data['a']
array([[1, 2, 3],
       [4, 5, 6]])
>>>data['b']
array([1, 2])
>>>data.close()

Post a Comment for "Converting Python Float To Bytes"