1

我正在编写一个代码来读取 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
4

0 回答 0