Skip to content

Instantly share code, notes, and snippets.

@nodamushi nodamushi/ihx16.py
Last active Jan 28, 2018

Embed
What would you like to do?
Convert decimal data file to intex hex file
#coding:utf-8
import sys
MAX_LINE_SIZE=32
a = []
if len(sys.argv) > 1:
with open(sys.argv[1],'r') as f:
for l in f:
a.append(int(l))
else:
for l in sys.stdin:
a.append(int(l))
length = len(a)
lines = (length+MAX_LINE_SIZE-1) % MAX_LINE_SIZE
for i in range(0,lines):
address = MAX_LINE_SIZE * i
datasize = MAX_LINE_SIZE if address+MAX_LINE_SIZE <= length else length -address
string = ":{0:02X}{1:04X}00".format(datasize,address)
s = (address + (address >> 8)+datasize) & 0xff
for k in range(address,address+datasize):
string = string + "{0:02X}".format(a[k])
s = (s + a[k])&0xff
string = string + "{0:02X}".format(256-s)
print(string)
print(":00000001FF")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.