I have binary string which is "00000000000000001011000001000010". I want to convert this string to byte array and from that byte array,I want to obtain corresponding float value. How it can be done in python?
I tried doing it using struct.unpack()
.
def bitstring_to_bytes(s):
v = int(s, 2)
b = bytearray()
while v:
b.append(v & 0xff)
v >>= 8
return bytes(b[::-1])
>>> s="00000000000000001011000001000010"
>>> print(bitstring_to_bytes(s))
>>> B
>>> struct.unpack('>f',B)
Also guide me on getting float value from byte array. Finally, we should obtain float value=88.0