-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2hex.py
executable file
·51 lines (36 loc) · 1.6 KB
/
bin2hex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
import sys,os,os.path,argparse
##########################################################################
##########################################################################
def main(options):
if options.output_fname=="-": o=sys.stdout
else: o=open(options.output_fname,"wt")
if options.input_fname=="-": i=sys.stdin
else: i=open(options.input_fname,"rb")
while True:
line=i.read(options.num_columns)
if line=="": break
for x in line:
o.write("0x%02X,"%ord(x))
o.write("\n")
# just let the files leak.
##########################################################################
##########################################################################
# http://stackoverflow.com/questions/25513043/python-argparse-fails-to-parse-hex-formatting-to-int-type
def auto_int(x): return int(x,0)
if __name__=="__main__":
parser=argparse.ArgumentParser(description="convert binary data into C source code")
parser.add_argument("-c",
dest="num_columns",
type=auto_int,
default=16,
help="specify number of bytes per line")
parser.add_argument("-o",
dest="output_fname",
metavar="FILE",
default="-",
help="file to save to, or \"-\" for stdout")
parser.add_argument("input_fname",
metavar="FILE",
help="file to read from, or \"-\" for stdin")
main(parser.parse_args(sys.argv[1:]))