2014-02-11 26 views
0

我有一個程序,必須能夠搜索二進制文件爲hex或ascii。我有ascii搜索/替換功能工作正常,但以前工作的十六進制搜索/替換不再工作。下面是正在發生的事情的一個例子:LH是在改變之後,十六進制被轉換爲「ascii層」,RH是原來的。 enter image description herepython無法獲得從十六進制到二進制的有效轉換

包含的代碼:

if a_chars>a_digits:alphex = 'a' 
else: alphex = 'h' 
print alphex 
# 3b. if alpha 
if alphex == 'a' : 
    data = open(tar,'rb').read(160); print('Opening tar file') 
    if len(old)>len(new): 
      old.ljust(len(old));pad = len(old)- len(new);new = new+(pad*' ') 
    if len(old)<len(new): 
     print 'NEW: data cannot exceed length of OLD:' 
# 3c. if hex 
if alphex == 'h' : 
    with open(tar,'rb') as f: 
      data = binascii.hexlify (f.read(100));print('Opening tar bfile') 
    if len(old)>len(new): print 'OLD>NEW hex size must be =' 
    if len(old)<len(new): print 'OLD<NEW hex size must be ='  
    old = old.lower();old = ''.join(old.split());print 'old: ', old 
    new = new.lower();new = ''.join(new.split());print 'new: ', new 
##sub write new file 
fo= open(tarname+time+'.'+tarext,'wb');print 'Creating new file...' 
fo.write (data.replace(old,new));print 'Writing file...' 

回答

0

給我在二月的追逐樹木圍繞我在森林裏迷路了第三Previous answer

data = data.replace(old,new);data = binascii.unhexlify(data) 
fo.write(data);print 'Writing bfile...' 

我忘記將數據重新分配回數據(data = binascii.unhexlify(data))以獲得校正的輸出。

相關問題