Skip to content

Instantly share code, notes, and snippets.

@phooky
Created September 7, 2010 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phooky/569140 to your computer and use it in GitHub Desktop.
Save phooky/569140 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Here's a useful skeleton for generating (in this case) an intel hex file
# for filling size bytes with 0xff; it can be easily modified to convert
# bytes from stdin.
size = 0x200
offset = 0
blocksize = 16
while size > 0:
codes = []
linesize = min(blocksize,size)
codes.append(linesize)
codes.append(offset>>8)
codes.append(offset&0xff)
codes.append(0) # data code
codes.extend([0xff] * linesize)
# calculate checksum
checksum=(0x100-(sum(codes)&0xff))&0xff
codes.append(checksum)
print reduce(lambda s,v:s+'{0:02X}'.format(v), codes, ":")
offset += linesize
size -= linesize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment