如何将十六进制字符串转换为浮点数(小端) [英] How to convert HEX string to Float (Little Endian)

查看:84
本文介绍了如何将十六进制字符串转换为浮点数(小端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习了 Python (3.x),但我一直坚持将 HEX 字符串转换为 Float.我有这个十六进制字符串值:<代码>'0x22354942F31AFA42CE6A494311518A43082CAF437C6BD4C35F78FA433BF10F442A5222448D3D3544200749C438295C40404B395C4404Bcode295C404B4B4B4B4B4B4B404B代码4B4B4084B代码我想把它变成浮动.

我曾尝试使用此代码:

bs=bytes.fromhex(row[2:])fmt = '<'+ ('H' * (len(bs)//2))res=struct.unpack(fmt, bs)

和它给我的<代码> 13602.0,16969.0,6899.0,17146.0,27342.0,17225.0,20753.0,17290.0,11272.0,17327.0,27516.0,50132.0,30815.0,17402.0,61755.0,17423.0,21034.0,17442.0,15757.0结果,17461.0,1824.0,50249.0,10552.0,17500.0,44904.0,17518.0,46086.0,17536.0,20816.0,17546.0,45091,59.0,45091,59.0, 8091,59.0, 20091,59.0, 8091.0, 30091.0, 20091.57.0检查后,我发现我目前拥有的代码以 16 为基数浮动,而我需要它以 32 为基数(或者可能不是,因为我不确定基数/格式),具有预期的浮动结果如<代码> 50.3018875,125.052635,201.4172,276.633331,350.344,424.839722,500.9404,575.7692,649.2838,724.961731,804.1113,880.644043,954.7407,1029.62573,106.541,1181.50427,1255.291 这是我从得到的值这<一个href="https://www.scadacore.com/tools/programming-calculators/online-hex-converter/" rel="nofollow noreferrer">计算器转换器.我应该在编码中更改哪些内容以获得预期结果?谢谢.

解决方案

让我们在这里分解一下,因为您似乎对所有表示的杂耍感到困惑.您有一些二进制数据的十六进制字符串(即 base 16 编码).那是你的 0x22354942F31AFA42CE6A494311....您正确地确定您可以使用 bytes.fromhex 将其从其编码形式转换为 python bytes:

<预> <代码> hex_encoded = '0x22354942F31AFA42CE6A494311518A43082CAF437C6BD4C35F78FA433BF10F442A5222448D3D3544200749C438295C4468AF6E4406B4804450518A4423B0934450E99CC4'binary_data = bytes.fromhex(hex_encoded[2:]) # 我们做 2: 删除前导 '0x'

此时,除非我们知道 binary_data 是如何构造的,否则我们什么也做不了.但我们可以做一些猜测.你知道前几个数字是浮点数:50.3018875, 125.052635, 201.4172, ....通常,浮点数使用 IEEE 754 标准进行编码.这提供了浮点数的 3 种不同编码:binary16(16 位)、float(32 位)和 double(64 位).您可以在 struct 文档 中看到这些,它们是格式代码分别为 'e''f''d'.我们可以尝试每一个看看你的二进制数据是哪一个(如果有的话)被编码为.通过反复试验,我们发现您的数据被编码为 32 位浮点数,因此您可以使用以下命令对其进行解码:

FLOAT = 'f'fmt = '<'+ FLOAT * (len(binary_data)//struct.calcsize(FLOAT))数字 = struct.unpack(fmt, binary_data)打印(数字)

为什么您尝试的方法不起作用?好吧,您使用了用于无符号短整型的格式代码 'H'.这是一个整数,这就是为什么你要返回没有小数部分的数字!

I just learned Python (3.x) and I am stuck with HEX String conversion to Float. I have this HEX String values: '0x22354942F31AFA42CE6A494311518A43082CAF437C6BD4C35F78FA433BF10F442A5222448D3D3544200749C438295C4468AF6E4406B4804450518A4423B0934450E99CC4' And I want to turn it into float.

I have tried to use this code:

bs=bytes.fromhex(row[2:])
fmt = '<' + ('H' * (len(bs) // 2))
res=struct.unpack(fmt, bs)

and it gives me the result of 13602.0,16969.0,6899.0,17146.0,27342.0,17225.0,20753.0,17290.0,11272.0,17327.0,27516.0,50132.0,30815.0,17402.0,61755.0,17423.0,21034.0,17442.0,15757.0,17461.0,1824.0,50249.0,10552.0,17500.0,44904.0,17518.0,46086.0,17536.0,20816.0,17546.0,45091.0,17555.0,59728.0,50332.0

After checking it, I found out that the code that what I currently have is float in base 16, while I need it in base 32 (or maybe not because I am not sure what base/format), with expected float results as 50.3018875, 125.052635,201.4172,276.633331,350.344,424.839722,500.9404,575.7692,649.2838,724.961731,804.1113,880.644043,954.7407,1029.62573,106.541,1181.50427,1255.291 the values which I got from this Calculator Converter. What should I change in the coding to get the expected results? Thank you.

解决方案

Let's break things down here, because you seem to be confused a bit with all of the juggling of representations. You have some hexadecimal string (that's base 16 encoding) of some binary data. That's your 0x22354942F31AFA42CE6A494311.... You correctly identified that you can convert this from its encoded form to python bytes with bytes.fromhex:

hex_encoded = '0x22354942F31AFA42CE6A494311518A43082CAF437C6BD4C35F78FA433BF10F442A5222448D3D3544200749C438295C4468AF6E4406B4804450518A4423B0934450E99CC4'
binary_data = bytes.fromhex(hex_encoded[2:])  # we do 2: to remove the leading '0x'

At this point, unless we know how binary_data was constructed we can't do anything. But we can take some guesses. You know the first few numbers are floating points: 50.3018875, 125.052635, 201.4172, .... Typically floats are encoded using the IEEE 754 standard. This provides 3 different encodings of a floating point number: binary16 (16 bits), float (32 bits), and double (64 bits). You can see these in the struct documentation, they are format codes 'e', 'f', and 'd', respectively. We can try each to see which of (if any) your binary data is encoded as. By trial and error, we discover your data was encoded as 32-bit floats, so you can decode them with:

FLOAT = 'f'
fmt = '<' + FLOAT * (len(binary_data) // struct.calcsize(FLOAT))
numbers = struct.unpack(fmt, binary_data)
print(numbers)

Why did what you tried not work? Well you used the format code 'H' which is for an unsigned short. This is an integer, which is why you were getting back numbers with no fractional part!

这篇关于如何将十六进制字符串转换为浮点数(小端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆