我正在编写一个代码,以读取2个十六进制文件并写出差异并将其转换(using binascii.unhexify()
)转换为十进制,以便我可以查看并进行更改。
我试图用相反的(binascii.hexify()
)将其转换回十六进制格式,但它给我的输出与预期的不同。
我尝试复制各行并将其转换回十六进制,这将为我提供所需的输出。
问题似乎出在读取文件并解析函数的各个行。
def read_file(fst, scd):
with open(fst) as f:
g = f.read().splitlines()
with open (scd) as ref:
ref_lines = ref.read().splitlines()
return g, ref_lines
def compare(run, sta):
with open("unum_change.txt", "w+") as diff:
for i in range(len(run)):
if run[i] != sta[i]:
diff.write(f"{binascii.unhexlify(g[i][1:])}\n")
问题在下面的代码中:
def trans ():
with open ('unum_change.txt', 'rb') as change, open ('reconv_change.txt', 'w') as file2:
contents = change.readlines()
for j in range(len(contents)):
file2.write(f"{binascii.hexlify(contents[j])}\n")
所需的输出应采用以下格式:
104A80000A0000000D0A0000554E49543A20303308
但是我得到的是这样的格式:
62275c7831304a5c7838305c7830305c6e5c7830305c7830305c7830305c725c6e5c7830305c783030554e49543a2030395c7427200d0a