简体   繁体   English

如何在python中转换浮点数?

[英]How to convert floating point number in python?

How to convert floating point number to base-16 numbers, 8 hexadecimal digits per 32-bit FLP number in python? 如何将浮点数转换为基数为16的数字,python中每32位FLP数为8个十六进制数?

eg : input = 1.2717441261e+20 output wanted : 3403244E 例如: input = 1.2717441261e+20 output wanted : 3403244E

If you want the byte values of the IEEE-754 representation, the struct module can do this: 如果需要IEEE-754表示的字节值, struct模块可以执行以下操作:

>>> import struct
>>> f = 1.2717441261e+20
>>> struct.pack('f', f)
'\xc9\x9c\xdc`'

This is a string version of the bytes, which can then be converted into a string representation of the hex values: 这是字节的字符串版本,然后可以将其转换为十六进制值的字符串表示形式:

>>> struct.pack('f', f).encode('hex')
'c99cdc60'

And, if you want it as a hex integer, parse it as such: 并且,如果您希望它作为十六进制整数,请将其解析为:

>>> s = struct.pack('f', f).encode('hex')
>>> int(s, 16)
3382500448

To display the integer as hex: 要将整数显示为十六进制:

>>> hex(int(s, 16))
'0xc99cdc60'

Note that this does not match the hex value in your question -- if your value is the correct one you want, please update the question to say how it is derived. 请注意,这与您问题中的十六进制值不匹配 - 如果您的值是您想要的正确值,请更新问题以说明它是如何派生的。

There are several possible ways to do so, but none of them leads to the result you wanted. 有几种可能的方法,但没有一种方法可以达到您想要的效果。

  • You can code this float value into its IEEE binary representation. 您可以将此浮点值编码为其IEEE二进制表示。 This leads indeed to a 32 bit number (if you do it with single precision). 这确实导致32位数(如果你用单精度进行)。 But it leads to different results, no matter which endianness I suppose: 但无论我认为哪种字节顺序,它都会导致不同的结果:

     import struct struct.pack("<f", 1.2717441261e+20).encode("hex") # -> 'c99cdc60' struct.pack(">f", 1.2717441261e+20).encode("hex") # -> '60dc9cc9' struct.unpack("<f", "3403244E".decode("hex")) # -> (687918336.0,) struct.unpack(">f", "3403244E".decode("hex")) # -> (1.2213533295835077e-07,) 
  • As the other one didn't fit result-wise, I'll take the other answers and include them here: 由于另一个不符合结果,我将采取其他答案并将其包含在这里:

     float.hex(1.2717441261e+20) # -> '0x1.b939919e12808p+66' 

    Has nothing to do with 3403244E as well, so maybe you want to clarify what exactly you mean. 3403244E无关,所以也许你想澄清一下你究竟是什么意思。

There are surely other ways to do this conversation, but unless you specify which method you want, no one is likely to be able to help you. 肯定还有其他方法可以进行此对话,但除非您指定所需的方法,否则没有人可以帮助您。

There is something wrong with your expected output : 您的预期输出有问题:

import struct
input = 1.2717441261e+20
buf = struct.pack(">f", input)
print ''.join("%x" % ord(c) for c in struct.unpack(">4c", buf) )

Output : 输出:

60dc9cc9

Try float.hex(input) . 尝试float.hex(input) This should convert a number into a string representing the number in base 16, and works with floats, unlike hex() . 这应该将一个数字转换为表示基数为16的数字的字符串,并且与hex()不同,它与浮点数一起使用。 The string will begin with 0x however, and will contain 13 digits after the decimal point, so I can't help you with the 8 digits part. 该字符串将以0x ,并且在小数点后将包含13位数字,因此我无法帮助您处理8位数部分。

Source: http://docs.python.org/2/library/stdtypes.html#float.hex 资料来源: http//docs.python.org/2/library/stdtypes.html#float.hex

如果input已经是浮点数,请尝试使用float.hex(input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM