Skip to content

Instantly share code, notes, and snippets.

@wavebeem
Created January 5, 2011 02:00
Show Gist options
  • Save wavebeem/765817 to your computer and use it in GitHub Desktop.
Save wavebeem/765817 to your computer and use it in GitHub Desktop.
Refactored hexy script
#!/usr/bin/python2
import sys
arg = sys.argv
if len(arg) != 3:
exit(1)
infile = arg[1]
outfile = arg[2]
try:
data = open(infile, 'rb').read()
except IOError:
sys.stderr.write('Binary file does not exist!\n')
exit(1)
name = outfile.upper()
out = open(outfile + '.h', 'w')
out.write(
'''#ifndef _%s_
#define _%s_
const unsigned char %s[] = {''' % (name, name, name)
)
for i, char in enumerate(data):
if (i % 8) == 0:
out.write('\n' + (' ' * 4))
out.write('0x%02x' % ord(char))
if i < (len(data) - 1):
out.write(', ')
out.write(
'''
};
#endif
'''
)
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment