Last active
January 28, 2018 16:48
-
-
Save nodamushi/2e72e1db508205b10f211923cc412b88 to your computer and use it in GitHub Desktop.
Convert decimal data file to intex hex file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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