Tre Rossi
Tre Rossi

Reputation: 11

how can i read a files hex content and write that to a hex file

so heres my current code

import binascii
with open(file, 'rb') as f:
    content = f.read()
f.close()


with open('wmhex.txt','w')as g:
    g.write(content)
g.close()

the problem is that I can only write the hex data of the old file into the hex data of the new file, essentially duplicating the file. I want to write it as text to the file, but I cant figure out how to handle the hex as a string.

Upvotes: 0

Views: 1271

Answers (1)

bashBedlam
bashBedlam

Reputation: 1500

Is this what you are looking for?

from binascii import hexlify

with open(file, 'rb') as f:
    content = f.read()
f.close()


with open('wmhex.txt','wb')as g:
    g.write(hexlify (content))
g.close()

Upvotes: 1

Related Questions