Skip to content

Instantly share code, notes, and snippets.

@peisenhower
Last active August 29, 2015 14:10
Show Gist options
  • Save peisenhower/e711fe1afde5e261bc79 to your computer and use it in GitHub Desktop.
Save peisenhower/e711fe1afde5e261bc79 to your computer and use it in GitHub Desktop.
xxd written in python for windows. Need to install IntelHex first.
#!/usr/bin/env python
from argparse import ArgumentParser
from intelhex import IntelHex
import os
def xxd(in_path, out_path, offset=0):
"""convert binary to hex"""
try:
hex = IntelHex()
hex.loadbin(in_path, offset)
hex.write_hex_file(out_path)
return 0
except Exception, e:
print e
return -1
def xxd_reverse(in_path, out_path):
"""convert hex file to binary"""
try:
hex = IntelHex(in_path)
hex.tobinfile(out_path)
return 0
except Exception, e:
print e
return -1
if __name__ == '__main__':
parser = ArgumentParser(description="Convert binary file to hex.")
parser.add_argument('infile', help='Input file in binary.')
parser.add_argument('--outfile', '-o', help='Output file in hex.',
required=False)
parser.add_argument('--offset', '-n',
help='Adress offset for the hex output.',
default="0", required=False)
parser.add_argument('--reverse', '-r', help='Output file in binary.',
required=False)
args = parser.parse_args()
if(args.outfile is None):
args.outfile = os.path.splitext(args.infile)[0] + ".hex"
args.offset = int(args.offset, 0)
if args.reverse:
xxd_reverse(args.infile, args.outfile)
else
xxd(args.infile, args.outfile, args.offset)
@peisenhower
Copy link
Author

No problem. I should be able to go to it this week. The input file is going to have to be intel hex for reverse to work. Not just a massive hex data file.

@peisenhower
Copy link
Author

Ill add an example too but in the mean time see below. Everything after the source file is optional. if you don't provide an output name it converts the input file to a .hex file.

python xxd.py source.bin -o output.hex -n 0x1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment