12
votes

So, what I am trying to do is convert a float to a bytearray but I keep on receiving both no input, and EXTREME slowing/freezing of my computer. My code is

import struct

def float_to_hex(f):
    return hex(struct.unpack('<I', struct.pack('<f', f))[0])
value = 5.1 #example value
...
value = bytearray(int(float_to_hex(float(value)), 16)

I found on another article a function to convert floats to hex which is

def float_to_hex(f):
    return hex(struct.unpack('<I', struct.pack('<f', f))[0])

and then I converted it from hex to an int. What is the problem with this? How could I better convert it from a float to bin or bytearray?

2
Why do you want a float as a bytearray? What are you trying to achieve? - cdarke
Is this to display the binary form of the float as hexadecimal text, or to get the raw bytes? Using hex implies you want hexadecimal text, not the raw bytes, and in that case, you wouldn't want a bytearray at all (which is a binary data type), you'd want str (the text type in Python 3). - ShadowRanger
Also, do you really want C float or C double representation? - ShadowRanger
What I am looking for is getting the raw bytes. I got the function while looking up a way to represent IEEE (754) floating point in hex from floats. I know it would be correct if I could plug the hex string into gregstoll.dyndns.org/~gregstoll/floattohex and get the original number. - tatatat0
@tatatat0: If you're entering this on a webpage, you don't want the raw bytes, you want the hex representation of said bytes; I can't reach that site (proxy blocking it), but web forms generally don't accept inputs of raw binary data, they take a hex encoded version. For the record, Python's float type is actually based on the C double type, not the C float type (which is typically half the precision of double), so you might want format code d, not f. As I mentioned in another comment, on 3.5, struct.pack("f", value).hex() should get you the hexadecimal text you want. - ShadowRanger

2 Answers

22
votes

It depends what you want, and what you are going to do with it. If all you want is a bytearray then:

import struct

value = 5.1

ba = bytearray(struct.pack("f", value))  

Where ba is a bytearray. However, if you wish to display the hex values (which I suspect), then:

print([ "0x%02x" % b for b in ba ])

EDIT:

This gives (for value 5.1):

['0x33', '0x33', '0xa3', '0x40']

However, CPython uses the C type double to store even small floats (there are good reasons for that), so:

value = 5.1
ba = bytearray(struct.pack("d", value))   
print([ "0x%02x" % b for b in ba ])

Gives:

['0x66', '0x66', '0x66', '0x66', '0x66', '0x66', '0x14', '0x40']
1
votes

The result I would want from 5.1 is 0x40 a3 33 33 or 64 163 51 51. Not as a string.

To get the desired list of integers from the float:

>>> import struct
>>> list(struct.pack("!f", 5.1))
[64, 163, 51, 51]

Or the same as a bytearray type:

>>> bytearray(struct.pack("!f", 5.1))
bytearray(b'@\xa333')

Note: the bytestring (bytes type) contains exactly the same bytes:

>>> struct.pack("!f", 5.1)
b'@\xa333'
>>> for byte in struct.pack("!f", 5.1):
...    print(byte)
...
64
163
51
51

The difference is only in mutability. list, bytearray are mutable sequences while bytes type represents an immutable sequence of bytes. Otherwise, bytes and bytearray types have a very similar API.