Skip to content

Instantly share code, notes, and snippets.

@noam1023
Created February 12, 2019 12:31
Show Gist options
  • Save noam1023/65a1a7f5fb7c3ccefac49228c1a8c0e6 to your computer and use it in GitHub Desktop.
Save noam1023/65a1a7f5fb7c3ccefac49228c1a8c0e6 to your computer and use it in GitHub Desktop.
print binary data as hex string for inclusion in source code
# read a binary file and output it in ASCII/hex
"""
$ python3 bin_to_hex.py --infile 00_00_00_400_L01.jpg --head 20
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
"""
import argparse
def parse(input, max_bytes):
i = 0
with open(input,"rb") as f:
while True:
b = f.read(1)
print("0x%s," % b.hex(), end=' ')
i +=1
if i >= max_bytes:
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='convert binary to code style hex')
parser.add_argument('--infile')
parser.add_argument('--head',type=int, default= 256)
args = parser.parse_args()
parse(args.infile, args.head)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment