Binary file reads and format using Python struct.unpack
#!/usr/bin/python # Program to read and format binary files. # 'rb' - read binary mode # Use of struct.unpack # Read 64k of memory locations. # Write only 3 X 256 byte pages. # Calling functions from a class. import sys import re import commands import struct class rbin: def file_op(self,rfile): self.readfile = rfile self.wfile = open('loadmem.txt', 'w') for line in self.readfile.readlines(): for cnt in range (0,4096,1): x = cnt*16 test = line[x:x+16] if (len(test) == 14): magic = struct.unpack("<14b",test) for i in range(0,14,1): self.wfile.write("0x%X\n" % (magic[i])) else: magic = struct.unpack("<16b",test) for i in range(0,16,1): if ((x <= 498) | ((x <= 65520) & (x >= 65280))): self.wfile.write("0x%X\n" % (magic[i])) self.readfile.close() self.wfile.close() # Call Class T = rbin() # Call function from class T.file_op(open('reg.bin','rb'))