Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用intelhex包在python中读取intelhex文件的非零部分_Python_Python 3.x_Hex_Hex File - Fatal编程技术网

如何使用intelhex包在python中读取intelhex文件的非零部分

如何使用intelhex包在python中读取intelhex文件的非零部分,python,python-3.x,hex,hex-file,Python,Python 3.x,Hex,Hex File,我正在尝试使用python 3.6的intelhex包从intelhex文件中读取部分十六进制数据。使用下面的代码,我打开了文件,并试图转换为字典,如果它有帮助 ih = IntelHex("data.hex") mydict = ih.todict() 现在我有地址了 startAddress = some value 我想读取从$startAddress开始的存储数据,并将其分隔为零值。执行该操作的最佳方法是什么?以下代码是解决方案。首先,我将IntenHex文件转换为字典: from

我正在尝试使用python 3.6的intelhex包从intelhex文件中读取部分十六进制数据。使用下面的代码,我打开了文件,并试图转换为字典,如果它有帮助

ih = IntelHex("data.hex")
mydict = ih.todict() 
现在我有地址了

startAddress = some value

我想读取从$startAddress开始的存储数据,并将其分隔为零值。执行该操作的最佳方法是什么?

以下代码是解决方案。首先,我将IntenHex文件转换为字典:

from intelhex import IntelHex
ih = IntelHex("sampleFile.hex")
ihdict = ih.todict()
然后在字典中从
startAddress
迭代到值为
None
的位置。我将值存储在名为
datastr
的字符串中

datastr = ""
startAddress = 500300
while ihdict.get(startAddress) != None:
    datastr += str("%0.2X" %ihdict.get(startAddress))
    startAddress += 1