You can do that with the struct
package:
import struct
def binary[num]:
return ''.join['{:0>8b}'.format[c] for c in struct.pack['!f', num]]
That packs it as a network byte-ordered float, and then converts each of the resulting bytes into an 8-bit binary representation and concatenates them out:
>>> binary[1]
'00111111100000000000000000000000'
Edit: There was a request to expand the explanation. I'll expand this using intermediate variables to comment each step.
def binary[num]:
# Struct can provide us with the float packed into bytes. The '!' ensures that
# it's in network byte order [big-endian] and the 'f' says that it should be
# packed as a float. Alternatively, for double-precision, you could use 'd'.
packed = struct.pack['!f', num]
print 'Packed: %s' % repr[packed]
# For each character in the returned string, we'll turn it into its corresponding
# integer code point
#
# [62, 163, 215, 10] = [ord[c] for c in '>\xa3\xd7\n']
integers = [ord[c] for c in packed]
print 'Integers: %s' % integers
# For each integer, we'll convert it to its binary representation.
binaries = [bin[i] for i in integers]
print 'Binaries: %s' % binaries
# Now strip off the '0b' from each of these
stripped_binaries = [s.replace['0b', ''] for s in binaries]
print 'Stripped: %s' % stripped_binaries
# Pad each byte's binary representation's with 0's to make sure it has all 8 bits:
#
# ['00111110', '10100011', '11010111', '00001010']
padded = [s.rjust[8, '0'] for s in stripped_binaries]
print 'Padded: %s' % padded
# At this point, we have each of the bytes for the network byte ordered float
# in an array as binary strings. Now we just concatenate them to get the total
# representation of the float:
return ''.join[padded]
And the result for a few examples:
>>> binary[1]
Packed: '?\x80\x00\x00'
Integers: [63, 128, 0, 0]
Binaries: ['0b111111', '0b10000000', '0b0', '0b0']
Stripped: ['111111', '10000000', '0', '0']
Padded: ['00111111', '10000000', '00000000', '00000000']
'00111111100000000000000000000000'
>>> binary[0.32]
Packed: '>\xa3\xd7\n'
Integers: [62, 163, 215, 10]
Binaries: ['0b111110', '0b10100011', '0b11010111', '0b1010']
Stripped: ['111110', '10100011', '11010111', '1010']
Padded: ['00111110', '10100011', '11010111', '00001010']
'00111110101000111101011100001010'
In this article, we will see how to convert floating-point value to binary. Binary uses two digits, 0 and 1. Also called as base 2 number system Each position in a binary number represents a 0 power of the base [2]. Last position in a binary number represents a x power of the base [2].
First, we take the integer part from the floating point value and convert it to binary then take fractional part and convert it to binary form and lastly combining both.
Let’s say we have the following floating point number −
22.625
Convert decimal 22 to binary 10110. Convert decimal 0.625 to binary 0.101. Combine integer and fraction to get the following output −
10110.101
Convert floating to binary by taking user input
Example
def floatoctal_convert[my_number, places = 3]: my_whole, my_dec = str[my_number].split["."] my_whole = int[my_whole] my_dec = int [my_dec] res = bin[my_whole].lstrip["0b"] + "." for x in range[places]: my_whole, my_dec = str[[my_decimal_converter[my_dec]] * 8].split["."] my_dec = int[my_dec] res += my_whole return res def my_decimal_converter[num]: while num > 1: num /= 10 return num n = input["Enter floating point value : \n"] p = int[input["Enter the number of decimal places of the result : \n"]] print[floatoctal_convert[n, places = p]]
Output
Enter floating point value : 2.34 Enter the number of decimal places of the result : 3 10.256
Updated on 12-Aug-2022 11:54:31
- Related Questions & Answers
- C# program to convert floating to binary
- Java program to convert floating to binary
- Python program to convert decimal to binary number
- Python Program to Convert Gray Code to Binary
- Python Program to Convert Binary to Gray Code
- Convert decimal to binary number in Python program
- C# Program to Convert Binary to Decimal
- C# Program to Convert Decimal to Binary
- 8085 program to convert gray to binary
- 8085 Program to convert ASCII to binary
- C# program to convert binary string to Integer
- C++ Program To Convert Decimal Number to Binary
- Java Program to convert from decimal to binary
- Java Program to convert int to binary string
- 8085 program to convert binary numbers to gray