binary to ascii
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import binascii filename = 'target_bin.xxx' f = open('{0}'.format(filename),'rb') fw = open('{0}'.format(filename)+'.ascii','w') while True: byte = f.read(1) if len(byte) == 0: break hex = binascii.hexlify(byte) fw.write(hex) f.close() fw.close() |
ascii to binary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import binascii import os filename = 'target_ascii.xxx.ascii' file_name, file_extension = os.path.splitext(filename) if file_extension != '.ascii' : print "ERROR : File extension" exit() f = open('{0}'.format(filename),'r') fw = open('a2b_'+'{0}'.format(file_name),'wb') while True : line = f.read(2) if len(line) == 0: break fw.write(struct.pack('B',int(line.split('\n')[0],16))) f.close() fw.close() |