简体   繁体   English

如何将十六进制字符串转换为Float(小尾数法)

[英]How to convert HEX string to Float (Little Endian)

I just learned Python (3.x) and I am stuck with HEX String conversion to Float. 我刚刚学习了Python(3.x),并且对将HEX String转换为Float感到困惑。 I have this HEX String values: '0x22354942F31AFA42CE6A494311518A43082CAF437C6BD4C35F78FA433BF10F442A5222448D3D3544200749C438295C4468AF6E4406B4804450518A4423B0934450E99CC4' And I want to turn it into float. 我有以下十六进制字符串值: '0x22354942F31AFA42CE6A494311518A43082CAF437C6BD4C35F78FA433BF10F442A5222448D3D3544200749C438295C4468AF6E4406B4804450518A4423B0934450E99CC4'

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 它给我的结果是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 . 经过检查后,我发现我当前拥有的代码在基数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这些是我从此Converter 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. 您有一些二进制数据的十六进制字符串(以16为基数的编码)。 That's your 0x22354942F31AFA42CE6A494311... . 那就是您的0x22354942F31AFA42CE6A494311... You correctly identified that you can convert this from its encoded form to python bytes with bytes.fromhex : 您正确地确定可以将其从编码形式转换为具有bytes.fromhex python bytes

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. 在这一点上,除非我们知道binary_data构造方式,否则我们将无能为力。 But we can take some guesses. 但是我们可以猜测一下。 You know the first few numbers are floating points: 50.3018875, 125.052635, 201.4172, ... . 您知道前几个数字是浮点数: 50.3018875, 125.052635, 201.4172, ... Typically floats are encoded using the IEEE 754 standard . 通常,浮点是使用IEEE 754标准编码的。 This provides 3 different encodings of a floating point number: binary16 (16 bits), float (32 bits), and double (64 bits). 这提供了3种不同的浮点数编码:binary16(16位),float(32位)和double(64位)。 You can see these in the struct documentation , they are format codes 'e' , 'f' , and 'd' , respectively. 您可以在结构文档中看到它们,它们分别是格式代码'e''f''d' 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: 通过反复试验,我们发现您的数据被编码为32位浮点数,因此您可以使用以下方法对其进行解码:

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. 好吧,您使用了格式代码'H' ,它是无符号的缩写。 This is an integer , which is why you were getting back numbers with no fractional part! 这是一个整数 ,这就是为什么您要取回没有小数部分的数字的原因!

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

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