import sys import csv d = []; with open('hex','rb') as csvfile: hr = csv.reader(csvfile) next(hr, None); for row in hr: d.append(int(row[1],16)) f = []; # Final List c = 0; g = 0; s = 0; ds = 0x10; # Data Size sa = 0x0000; # Start Address rt = 0x00; # Record Type for i in range(len(d)): if d[i] == 0x0c: g = s = 1; if i < len(d)-1: if d[i] == 0x10 and d[i+1] == 0x14: s = 0; if g and s: if c == 0: l = []; l.append(":"); l.append(ds); l.append(sa); l.append(rt); c+=1; l.append(d[i]); if c >= 16: l.append((-sum(l[1:]) & 0x0ff)) f.append(l); sa +=0x10; c=0; if s == 0 and d[i] == 0x14: s = 1; for l in f: for i in range(len(l)): if i == 0: sys.stdout.write(l[i]); elif i == 2: sys.stdout.write("%04x"%l[i]); else: sys.stdout.write("%02x"%l[i]); print