why does my code give me different results than expected

132 Views Asked by At

I am working on a code to read through 2 hex files and write out the difference and convert (using binascii.unhexify()) it to decimal so i can view it and make changes.

I tried to convert it back to hex format with the reverse (binascii.hexify()) but it is giving me a different output from what i expected.

I tried copying the individual lines and converting it back to hex and it gives me the desired output.

The problem seems to be with reading the file and parsing the individual lines to the function.

    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") 

The issue is in the below code:

    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")

Desired output should be in this format:

104A80000A0000000D0A0000554E49543A20303308

But instead I am getting a format like this:

62275c7831304a5c7838305c7830305c6e5c7830305c7830305c7830305c725c6e5c7830305c783030554e49543a2030395c7427200d0a
0

There are 0 best solutions below