dee*_*dee 12 python python-3.x
我想将二进制数转换为浮点数.这是一个可能性的例子:
>>> float(-0b1110)
Run Code Online (Sandbox Code Playgroud)
给我正确的输出:
-14.0
Run Code Online (Sandbox Code Playgroud)
不幸的是,我正在处理二进制字符串,即我需要类似的东西float('-0b1110').
但是,这不起作用:
>>> float('-0b1110')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): -0b1110
Run Code Online (Sandbox Code Playgroud)
我试图使用binascii.a2b_qp(string[, header])它将一个引用可打印数据块转换回二进制并返回二进制数据.但最终,我得到了同样的错误:
>>> float(binascii.a2b_qp('-0b1110'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): -0b1110
Run Code Online (Sandbox Code Playgroud)
我理解输出数是一个整数的情况,但如果我想获得数字12.546呢?该函数对二进制字符串的调用是什么样的呢?
mar*_*eau 13
在您的一条评论中,您指出二进制数表示8字节长IEEE 754二进制64格式的浮点数.然而,这与-0b1110您作为示例显示的值不一致,所以我忽略了它,并使用我自己的格式作为示例输入数据,用于测试下面显示的答案.
基本上所做的是首先将二进制字符串转换为整数值,然后将其转换为原始字节字符串,并将其传递给struct.unpack()最终转换为浮点值.下面bin_to_float()显示的功能驱动了该过程.尽管未示出,但二进制输入字符串参数可以带有前缀'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 << 1) % n, 'hex')[-length:]
def float_to_bin(value): # For testing.
""" Convert float to 64-bit binary string. """
[d] = struct.unpack(">Q", struct.pack(">d", value))
return '{:064b}'.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)
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
这对我有用。用 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
Run Code Online (Sandbox Code Playgroud)
另一种选择是做
from ast import literal_eval
float_str = "-0b101010101"
result = float(literal_eval(float_str))
Run Code Online (Sandbox Code Playgroud)
与内置的"eval"不同,literal_eval甚至可以在用户输入上运行,因为它只能解析Python文字 - 并且不会执行表达式,这意味着它也不会调用函数.
float(int('-0b1110',0))
Run Code Online (Sandbox Code Playgroud)
这对我行得通.
如果你有一个表示浮点数而不是整数的64位字符串,你可以进行三步转换 - 第一步将字符串转换为整数,第二步将其转换为8字节字符串,第三个将这些位重新解释为浮点数.
>>> import struct
>>> s = '0b0100000000101001000101111000110101001111110111110011101101100100'
>>> q = int(s, 0)
>>> b8 = struct.pack('Q', q)
>>> struct.unpack('d', b8)[0]
12.546
Run Code Online (Sandbox Code Playgroud)
当然,您可以将所有这些步骤合并为一行.
>>> s2 = '0b1100000000101100000000000000000000000000000000000000000000000000'
>>> struct.unpack('d', struct.pack('Q', int(s2, 0)))[0]
-14.0
Run Code Online (Sandbox Code Playgroud)