1

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
delandy
  • 11
  • 3
  • The *format* appears to be hex in both expected and desired output - uppercase in the first, lowercase in the latter case. Do you actually mind the different *format* or rather that it is completely different content? – MisterMiyagi Oct 28 '19 at 10:09
  • @MisterMiyagi well i don't like the second hex format i received. I'd prefer the former as that is the format in my main hex file i am working on. Besides when for example, I used the individual lines of the read file and convert it myself using this code ' new = binascii.hexlify(b'\x10J\x80\x00\n\x00\x00\x00\r\n\x00\x00UNIT: 02\t') print(new.upper()) ' i get the desired output i want. So what is wrong with the reading of the file that makes it give me the hex format of the latter. – delandy Oct 28 '19 at 10:30

0 Answers0