[英]Convert hex string to float
I am trying to read from a file a bunch of hex numbers.我正在尝试从文件中读取一堆十六进制数字。
lines ='4005297103CE40C040059B532A7472C440061509BB9597D7400696DBCF1E35CC4007206BB5B0A67B4007AF4B08111B87400840D4766460524008D47E0FFB4ABA400969A572EBAFE7400A0107CCFDF50E'
dummy = [lines[index][i:i+16] for i in range(0, len(lines[index]),16)]
rdummy=[]
for elem in dummy[:-1]:
rdummy.append(int(elem,16))
these are 10 number of 16 digits in particular when reading the first one, I have:这些是 10 个 16 位数字,特别是在阅读第一个数字时,我有:
print(dummy[0])
4005297103CE40C0
now I would like to convert it to float现在我想将它转换为浮动
I have an IDL script that when reading this number gives 2.64523509我有一个 IDL 脚本,当读取这个数字时给出 2.64523509
the command used in IDL is IDL 中使用的命令是
double(4613138958682833088,0)
where it appers 0 is an offset used when converting.其中 0 是转换时使用的偏移量。
is there a way to do this in python?有没有办法在 python 中做到这一点?
you probably want to use the struct
package for this, something like this seems to work:您可能想为此使用
struct
package ,这样的事情似乎有效:
import struct
lines ='4005297103CE40C040059B532A7472C440061509BB9597D7400696DBCF1E35CC4007206BB5B0A67B4007AF4B08111B87400840D4766460524008D47E0FFB4ABA400969A572EBAFE7400A0107CCFDF50E'
for [value] in struct.iter_unpack('>d', bytes.fromhex(lines)):
print(value)
results in 2.64523509
being printed first which seems about right导致
2.64523509
被打印,这似乎是正确的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.