Skip to content

Instantly share code, notes, and snippets.

@Und3rf10w
Created October 26, 2019 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Und3rf10w/15fe32226fb048b24f3d420e6ee6a4ca to your computer and use it in GitHub Desktop.
Save Und3rf10w/15fe32226fb048b24f3d420e6ee6a4ca to your computer and use it in GitHub Desktop.
Convert a binary file to a hexbyte string
#!/usr/bin/env python3
import binascii
import argparse
from os import path
parser = argparse.ArgumentParser(description ='Convert input raw binary file to a hex byte string')
parser.add_argument('-i', '--input', dest='input_file', required=True, help="The input file to convert")
parser.add_argument('-o', '--output', dest='output_file', required=False, help="The output file to write to")
args = parser.parse_args()
if path.exists(args.input_file):
with open(args.input_file, 'rb') as f:
content = f.read()
if args.output_file:
try:
o = open(args.output_file, 'w')
o.write(binascii.hexlify(content))
o.close()
except Exception as e:
print("ERROR: %s") % (str(e))
else:
print(binascii.hexlify(content))
f.close()
else:
print("ERROR: Provided input file does not exist!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment