Python bin to float

converting binary string into float

I’m not sure what you mean by «Convert the bits into a float». Do you want to do the equivalent of a C typecast (i.e. have the system interpret the sequence of bits as if it were a float)? Also, if this is a homework problem, you should add the [homework] tag.

Thank you for understanding. It is essentially a C typecast. This is not a homework problem though. I just have no idea how to do this.

How do you want to interpret them as a float ? As a number between 0 and 1? Two’s compliment? You need to be more specific? struct only works for floats in IEEE 754 binary format.

I would like to interpret them using the IEEE 754 format. When I try using struct to unpack it I am told that the string can only be 4 characters long. Doesn’t the standard require 32 bits?

You are not storing bits. You are storing integers that happen to have the values 0 or 1. Forget this at your peril.

2 Answers 2

Here is a solution that works. as_float32 could be extended to as_float64 by replacing «I» with «L» and «f» with «d». See the struct documentation for an explanation.

def as_float32(self): """ See: http://en.wikipedia.org/wiki/IEEE_754-2008 """ from struct import pack,unpack s = self.bitlist return unpack("f",pack("I", bits2int(s))) # Where the bits2int function converts bits to an integer. def bits2int(bits): # You may want to change ::-1 if depending on which bit is assumed # to be most significant. bits = [int(x) for x in bits[::-1]] x = 0 for i in range(len(bits)): x += bits[i]*2**i return x 

Источник

Читайте также:  Python build ext inplace

How to convert a binary (string) into a float value?

In one of your comments you indicated that the binary number represents a float in 8 byte long IEEE 754 binary64 format. However that is inconsistent with the -0b1110 value you showed as an example, so I’ve ignored it and used my own which is in the proper format as example input data for testing the answer shown below.

Essentially what is done is first the binary string is converted into an integer value, then next into a string of raw bytes which is passed to struct.unpack() for final conversion to a floating point value. The bin_to_float() function shown below drives the process. Although not illustrated, binary input string arguments can be prefixed with ‘0b’ .

from codecs import decode import struct def bin_to_float(b): """ Convert binary string to a float. """ bf = int_to_bytes(int(b, 2), 8) # 8 bytes needed for IEEE 754 binary64. return struct.unpack('>d', bf)[0] def int_to_bytes(n, length): # Helper function """ Int/long to byte string. Python 3.2+ has a built-in int.to_bytes() method that could be used instead, but the following works in earlier versions including 2.x. """ return decode('%%0%dx' % (length Q", struct.pack(">d", value)) return ''.format(d) if __name__ == '__main__': for f in 0.0, 1.0, -14.0, 12.546, 3.141593: print('Test value: %f' % f) binary = float_to_bin(f) print(' float_to_bin: %r' % binary) floating_point = bin_to_float(binary) # Round trip. print(' bin_to_float: %f\n' % floating_point) 
Test value: 0.000000 float_to_bin: '0000000000000000000000000000000000000000000000000000000000000000' bin_to_float: 0.000000 Test value: 1.000000 float_to_bin: '0011111111110000000000000000000000000000000000000000000000000000' bin_to_float: 1.000000 Test value: -14.000000 float_to_bin: '1100000000101100000000000000000000000000000000000000000000000000' bin_to_float: -14.000000 Test value: 12.546000 float_to_bin: '0100000000101001000101111000110101001111110111110011101101100100' bin_to_float: 12.546000 Test value: 3.141593 float_to_bin: '0100000000001001001000011111101110000010110000101011110101111111' bin_to_float: 3.141593 

This works for me. Tested with Python3.4:

def float_to_bin(num): return bin(struct.unpack('!I', struct.pack('!f', num))[0])[2:].zfill(32) def bin_to_float(binary): return struct.unpack('!f',struct.pack('!I', int(binary, 2)))[0] float_to_bin(bin_to_float(float_to_bin(123.123))) == float_to_bin(123.123) >>> True 

Источник

Оцените статью