Python 如何将浮点数转换为十六进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23624212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a float into hex
提问by user2339945
In Python I need to convert a bunch of floats into hexadecimal. It needs to be zero padded (for instance, 0x00000010 instead of 0x10). Just like http://gregstoll.dyndns.org/~gregstoll/floattohex/does. (sadly i can't use external libs on my platform so i can't use the one provided on that website)
在 Python 中,我需要将一堆浮点数转换为十六进制。它需要补零(例如,0x00000010 而不是 0x10)。就像http://gregstoll.dyndns.org/~gregstoll/floattohex/一样。(遗憾的是我不能在我的平台上使用外部库,所以我不能使用该网站上提供的库)
What is the most efficient way of doing this?
这样做的最有效方法是什么?
回答by Jonathon Reinhart
This is a bit tricky in python, because aren't looking to convert the floating-point valueto a (hex) integer. Instead, you're trying to interpretthe IEEE 754binary representation of the floating-point value as hex.
这在 python 中有点棘手,因为不打算将浮点值转换为(十六进制)整数。相反,您试图将浮点值的IEEE 754二进制表示解释为十六进制。
We'll use the pack
and unpack
functions from the built-in struct
library.
我们将使用内置库中的pack
和unpack
函数struct
。
A float
is 32-bits. We'll first pack
it into a binary1string, and then unpack
it as an int
.
Afloat
是 32 位。我们首先将pack
其转换为二进制1字符串,然后将unpack
其转换为int
.
def float_to_hex(f):
return hex(struct.unpack('<I', struct.pack('<f', f))[0])
float_to_hex(17.5) # Output: '0x418c0000'
We can do the same for double
, knowing that it is 64 bits:
我们可以对 做同样的事情double
,知道它是 64 位:
def double_to_hex(f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
double_to_hex(17.5) # Output: '0x4031800000000000L'
1 - Meaning a string of raw bytes; nota string of ones and zeroes.
1 - 表示一串原始字节;不是一串一和零。
回答by ghostarbeiter
In Python float
is always double-precision.
在 Pythonfloat
中总是双精度的。
If you require your answer to be output in the form of a hexadecimal integer, the question was already answered:
如果你要求你的答案以十六进制整数的形式输出,这个问题已经回答了:
import struct
# define double_to_hex as in the other answer
double_to_hex(17.5) # Output: '0x4031800000000000'
double_to_hex(-17.5) # Output: '0xc031800000000000'
However you might instead consider using the builtin function:
但是,您可能会考虑使用内置函数:
(17.5).hex() # Output: '0x1.1800000000000p+4'
(-17.5).hex() # Output: '-0x1.1800000000000p+4'
# 0x1.18p+4 == (1 + 1./0x10 + 8./0x100) * 2**4 == 1.09375 * 16 == 17.5
This is the same answer as before, just in a more structured and human-readable format.
这与之前的答案相同,只是采用了更加结构化和人类可读的格式。
The lower 52 bits are the mantissa. The upper 12 bits consists of a sign bit and an 11-bit exponent; the exponent bias is 1023 == 0x3FF, so 0x403 means '4'. See Wikipedia article on IEEE floating point.
低 52 位是尾数。高 12 位由符号位和 11 位指数组成;指数偏差是 1023 == 0x3FF,所以 0x403 表示“4”。请参阅有关 IEEE 浮点的维基百科文章。
回答by Ken
Further to Jonathon Reinhart'svery helpful answer. I needed this to send a floating point number as bytes over UDP
继乔纳森·莱因哈特 (Jonathon Reinhart) 的非常有用的回答之后。我需要这个来通过 UDP 发送一个浮点数作为字节
import struct
# define double_to_hex (or float_to_hex)
def double_to_hex(f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])
# On the UDP transmission side
doubleAsHex = double_to_hex(17.5)
doubleAsBytes = bytearray.fromhex(doubleAsHex.lstrip('0x').rstrip('L'))
# On the UDP receiving side
doubleFromBytes = struct.unpack('>d', doubleAsBytes)[0] # or '>f' for float_to_hex
回答by Kevin Cando
if you are on micropython (which is not said in the question, but I had trouble finding) you can use this
如果您使用的是 micropython(问题中没有说,但我找不到),您可以使用它
import struct
import binascii
def float_to_hex(f):
binascii.hexlify(struct.pack('<f', f))
float_to_hex(17.5) # 0x418c0000