如何在Python中将64位转换为双倍小数?

时间:2016-04-24 22:26:09

标签: python floating-point

使用Python,如何从64位位模式中获取十进制浮点值?我想我需要使用struct,但我怎样才能正确使用它?这是我尝试过的:

struct.unpack('<d','0100000011111000011010100000000000000000000000000000000000000000')

error: unpack requires a string argument of length 8

2 个答案:

答案 0 :(得分:2)

您可以这样做:

from numpy import * #for Double data type
double(int('0100000011111000011010100000000000000000000000000000000000000000',2))

答案 1 :(得分:0)

int_data = int('0100000011111000011010100000000000000000000000000000000000000000',2)
double_data = struct.unpack('d', struct.pack('q', int_data))[0]