from intelhex import IntelHex
program = """
// Bootloader data array
bootloader[] = {
%s
}
// Main program
void main() {
...
}
"""
ih = IntelHex()
ih.fromfile('bootloader.hex', format='hex')
maxaddr = int(ih.maxaddr() / 4 + 1) * 4
textformat = ' ' + '0x{:02x}, ' * 4
textdata = [textformat.format(ih[i], ih[i+1], ih[i+2], ih[i+3]) for i in range(0, maxaddr, 4)]
with open('bootloader.c', 'wt') as fo:
fo.write(program % ('\n'.join(textdata)))