将十六进制转换为浮点数

发布于 2024-08-08 02:13:58 字数 186 浏览 6 评论 0原文

如何在Python中将以下十六进制字符串转换为浮点数(单精度32位)?

"41973333" -> 1.88999996185302734375E1

"41995C29" -> 1.91700000762939453125E1

"470FC614" -> 3.6806078125E4

How to convert the following hex string to float (single precision 32-bit) in Python?

"41973333" -> 1.88999996185302734375E1

"41995C29" -> 1.91700000762939453125E1

"470FC614" -> 3.6806078125E4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

想念有你 2024-08-15 02:13:58

Python 3中:

>>> import struct
>>> struct.unpack('!f', bytes.fromhex('41973333'))[0]
18.899999618530273
>>> struct.unpack('!f', bytes.fromhex('41995C29'))[0]
19.170000076293945
>>> struct.unpack('!f', bytes.fromhex('470FC614'))[0]
36806.078125

Python 2中:

>>> import struct
>>> struct.unpack('!f', '41973333'.decode('hex'))[0]
18.899999618530273
>>> struct.unpack('!f', '41995C29'.decode('hex'))[0]
19.170000076293945
>>> struct.unpack('!f', '470FC614'.decode('hex'))[0]
36806.078125

In Python 3:

>>> import struct
>>> struct.unpack('!f', bytes.fromhex('41973333'))[0]
18.899999618530273
>>> struct.unpack('!f', bytes.fromhex('41995C29'))[0]
19.170000076293945
>>> struct.unpack('!f', bytes.fromhex('470FC614'))[0]
36806.078125

In Python 2:

>>> import struct
>>> struct.unpack('!f', '41973333'.decode('hex'))[0]
18.899999618530273
>>> struct.unpack('!f', '41995C29'.decode('hex'))[0]
19.170000076293945
>>> struct.unpack('!f', '470FC614'.decode('hex'))[0]
36806.078125
苍白女子 2024-08-15 02:13:58

我建议使用 ctypes 模块,它基本上可以让您使用低级数据类型。在您的情况下,您可以说

from ctypes import *

def convert(s):
    i = int(s, 16)                   # convert from hex to a Python int
    cp = pointer(c_int(i))           # make this into a c integer
    fp = cast(cp, POINTER(c_float))  # cast the int pointer to a float pointer
    return fp.contents.value         # dereference the pointer, get the float

print convert("41973333")    # returns 1.88999996185302734375E1

print convert("41995C29")    # returns 1.91700000762939453125E1

print convert("470FC614")    # returns 3.6806078125E4

我相信 ctypes 模块在这里有意义,因为您本质上是在问如何执行低级位转换。你的问题基本上是,我如何告诉Python获取一些数据并解释该数据,就好像这些完全相同的位是不同的数据类型一样?

在 C 中,如果你有一个 int 并想将其位解释为浮点数,你会做大致相同的事情,获取一个指针,然后对其进行转换和取消引用:

int i = 0x41973333;
float f = *((float*)&i);

这正是使用 ctypes< 的 Python 代码。 /code> 库在我的示例中正在执行。

I recommend using the ctypes module which basically lets you work with low level data types. In your case you could say

from ctypes import *

def convert(s):
    i = int(s, 16)                   # convert from hex to a Python int
    cp = pointer(c_int(i))           # make this into a c integer
    fp = cast(cp, POINTER(c_float))  # cast the int pointer to a float pointer
    return fp.contents.value         # dereference the pointer, get the float

print convert("41973333")    # returns 1.88999996185302734375E1

print convert("41995C29")    # returns 1.91700000762939453125E1

print convert("470FC614")    # returns 3.6806078125E4

I believe that the ctypes module makes sense here, because you're essentially asking how to perform low-level bit casting. Your question is basically, how do I tell Python to take some data and interpret that data as if those exact same bits were a different data type?

In C if you had an int and wanted to interpret its bits as a float, you'd do roughly the same thing, taking a pointer and then casting and dereferencing it:

int i = 0x41973333;
float f = *((float*)&i);

and that's exactly what the Python code using the ctypes library is doing in my example.

红焚 2024-08-15 02:13:58

我猜这个问题与 this one 有关,并且您正在使用 4 个字节而不是 8 个字节十六进制数字。

"\x41\x91\x33\x33" 是一个 4 字节字符串,即使它看起来像 16

>>> len("\x41\x91\x33\x33")
4
>>> import struct  
>>> struct.unpack(">fff","\x41\x97\x33\x33\x41\x99\x5C\x29\x47\x0F\xC6\x14")
(18.899999618530273, 19.170000076293945, 36806.078125)

如果您确实需要处理十六进制数字字符串而不是实际字节,您可以使用 struct.pack 来转换它,就像这样

>>> for hx in ["41973333","41995C29","470FC614"]:
...     print(struct.unpack(">f",struct.pack(">i",int(hx,16)))[0])
... 
18.8999996185
19.1700000763
36806.078125

I'm guessing this question relates to this one and you are working with 4 bytes rather than 8 hex digits.

"\x41\x91\x33\x33" is a 4 byte string even though it looks like 16

>>> len("\x41\x91\x33\x33")
4
>>> import struct  
>>> struct.unpack(">fff","\x41\x97\x33\x33\x41\x99\x5C\x29\x47\x0F\xC6\x14")
(18.899999618530273, 19.170000076293945, 36806.078125)

If you do need to deal with the string of hexdigits rather than the actual bytes, you can use struct.pack to convert it, like this

>>> for hx in ["41973333","41995C29","470FC614"]:
...     print(struct.unpack(">f",struct.pack(">i",int(hx,16)))[0])
... 
18.8999996185
19.1700000763
36806.078125
只怪假的太真实 2024-08-15 02:13:58

将十六进制字符串切成 2 个字符的块(字节),使用 int 格式将每个块转换为正确的字节,完成后进行 struct.unpack。即:

import struct 

testcases = {
"41973333": 1.88999996185302734375E1,
"41995C29": 1.91700000762939453125E1,
"470FC614": 3.6806078125E4,
}

def hex2float(s):
    bins = ''.join(chr(int(s[x:x+2], 16)) for x in range(0, len(s), 2))
    return struct.unpack('>f', bins)[0]

for s in testcases:
  print hex2float(s), testcases[s]

根据需要发射:

18.8999996185 18.8999996185
19.1700000763 19.1700000763
36806.078125 36806.078125

Slice up the hex strings into 2-character chunks (bytes), make each chunk into the right byte with int formatting, struct.unpack when done. I.e.:

import struct 

testcases = {
"41973333": 1.88999996185302734375E1,
"41995C29": 1.91700000762939453125E1,
"470FC614": 3.6806078125E4,
}

def hex2float(s):
    bins = ''.join(chr(int(s[x:x+2], 16)) for x in range(0, len(s), 2))
    return struct.unpack('>f', bins)[0]

for s in testcases:
  print hex2float(s), testcases[s]

emitting, as desired:

18.8999996185 18.8999996185
19.1700000763 19.1700000763
36806.078125 36806.078125
夜空下最亮的亮点 2024-08-15 02:13:58

当使用字符串时,无需对值进行切片或索引。

import struct
...
q = int('0x425c0000',16)
b8 = struct.pack('i', q)
dec, = struct.unpack('f', b8)

When working from string and without having to slice or index values.

import struct
...
q = int('0x425c0000',16)
b8 = struct.pack('i', q)
dec, = struct.unpack('f', b8)
玩物 2024-08-15 02:13:58

先生们...看哪:

    class fl:
        def __init__(this, value=0, byte_size=4):

            this.value = value

            if this.value: # speedy check (before performing any calculations)
                Fe=((byte_size*8)-1)//(byte_size+1)+(byte_size>2)*byte_size//2+(byte_size==3)
                Fm,Fb,Fie=(((byte_size*8)-(1+Fe)), ~(~0<<Fe-1), (1<<Fe)-1)

                FS,FE,FM=((this.value>>((byte_size*8)-1))&1,(this.value>>Fm)&Fie,this.value&~(~0 << Fm))
                if FE == Fie: this.value=(float('NaN') if FM!=0 else (float('+inf') if FS else float('-inf')))
                else: this.value=((pow(-1,FS)*(2**(FE-Fb-Fm)*((1<<Fm)+FM))) if FE else pow(-1,FS)*(2**(1-Fb-Fm)*FM))

                del Fe; del Fm; del Fb; del Fie; del FS; del FE; del FM

            else: this.value = 0.0

    print fl( 0x41973333 ).value # >>> 18.899999618530273
    print fl( 0x41995C29 ).value # >>> 19.170000076293945
    print fl( 0x470FC614 ).value # >>> 36806.078125
    print fl( 0x00800000 ).value # >>> 1.1754943508222875e-38 (minimum float value)
    print fl( 0x7F7FFFFF ).value # >>> 340282346638528859811704183484516925440L (maximum float value)
    # looks like I've found a small bug o.o
    # the code still works though (the numbers are properly formatted)
    # the result SHOULD be: 3.4028234663852886e+38 (rounded)
    print fl( 0x3f80000000, 5 ).value # >>> 1.0

抱歉最后的小“.value”...
这段代码已经在我的程序中用作类近两年了。
(只需稍加编辑,您就可以轻松地将其变成一个函数)

代码归功于 DaniWeb 上的 PyTony。

与非动态计算不同,
代码没有硬连接到固定的浮动大小,
并适用于任何字节大小。

尽管我想我们仍然有一些错误需要解决。 XDD
(我稍后会(如果可以的话)通过更新编辑此代码)

不过现在一切都很好......
我用它转换 3D 游戏模型没有遇到任何问题。 :)

Gentelmen... Behold:

    class fl:
        def __init__(this, value=0, byte_size=4):

            this.value = value

            if this.value: # speedy check (before performing any calculations)
                Fe=((byte_size*8)-1)//(byte_size+1)+(byte_size>2)*byte_size//2+(byte_size==3)
                Fm,Fb,Fie=(((byte_size*8)-(1+Fe)), ~(~0<<Fe-1), (1<<Fe)-1)

                FS,FE,FM=((this.value>>((byte_size*8)-1))&1,(this.value>>Fm)&Fie,this.value&~(~0 << Fm))
                if FE == Fie: this.value=(float('NaN') if FM!=0 else (float('+inf') if FS else float('-inf')))
                else: this.value=((pow(-1,FS)*(2**(FE-Fb-Fm)*((1<<Fm)+FM))) if FE else pow(-1,FS)*(2**(1-Fb-Fm)*FM))

                del Fe; del Fm; del Fb; del Fie; del FS; del FE; del FM

            else: this.value = 0.0

    print fl( 0x41973333 ).value # >>> 18.899999618530273
    print fl( 0x41995C29 ).value # >>> 19.170000076293945
    print fl( 0x470FC614 ).value # >>> 36806.078125
    print fl( 0x00800000 ).value # >>> 1.1754943508222875e-38 (minimum float value)
    print fl( 0x7F7FFFFF ).value # >>> 340282346638528859811704183484516925440L (maximum float value)
    # looks like I've found a small bug o.o
    # the code still works though (the numbers are properly formatted)
    # the result SHOULD be: 3.4028234663852886e+38 (rounded)
    print fl( 0x3f80000000, 5 ).value # >>> 1.0

sorry for the little ".value" at the end...
this code has been used as a class in my program for nearly 2 years now.
(with a little editing, you can easily make it into a function)

credit to PyTony over at DaniWeb for the code.

unlike non-dynamic computing,
the code is not hard-wired to a fixed float size,
and works with any byte-size.

though I guess we still have a few bugs to work out. XDD
(I'll edit this code later (if I can) with the update)

all is good though for now though...
I havn't had a problem converting 3D game models with it. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文