Search code examples
pythonstructhexpackdata-conversion

python text to hex and searching hex data or convert everything to binary


Decided to do a complete remake of the question. Hexedit in its default settings displays hex in the center window and ansi text in the right window. The below is the current code:

patlst = [line.strip() for line in open(patch,'rb',1)] #Read Patch start
if alphex == 'h' :
    old = patlst[patlst.index('OLD:')+1] #get old data str
    new = patlst[patlst.index('NEW:')+1] #get new data str
    old = old.lower();old = ''.join(old.split())
    new = new.lower();new = ''.join(new.split())
pircwd = os.chdir('..'); pircwd = os.getcwd() ##DIR change
with open(tar, 'rb') as f:
    data = binascii.hexlify(f.read(160))
with open(tar+'BAK', 'wb') as f:
    f.write(data.replace(b'old',b'new'))

Original hexedit view unaltered: This is the original hexedit

Desired result:

This is the desired result

Actual result:

enter image description here

I believe this requires the pack and unpack function to work properly? Or is there a better way all in all to do this?


Solution

  • To convert from (ASCII-encoded) hex to binary, you can use binascii.unhexlify().