forked from raspberrypi/pico-bootrom-rp2040
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2hex
executable file
·42 lines (35 loc) · 904 Bytes
/
bin2hex
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
#!/usr/bin/env python3
import sys
import re
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit("Usage: bin2hex [-<width>] <infile|-> <outfile|->\ne.g. bin2hex -32 rom.bin rom.hex")
size = 32
argn = 1;
m = re.match(r"-(\d+)", sys.argv[argn])
if m:
size = int(m.group(1))
argn = argn + 1
if size <= 0 or size % 8 != 0:
sys.exit("Size must be a multiple of 8 (got {})".format(size))
if sys.argv[argn] == '-':
ifile = sys.stdin.buffer
else:
ifile = open(sys.argv[argn], "rb")
argn = argn + 1
if sys.argv[argn] == '-':
ofile = sys.stdout
else:
ofile = open(sys.argv[argn], "w")
argn = argn + 1
bytes_per_word = size // 8
while True:
bytes = ifile.read(bytes_per_word)
if not bytes:
break
word = ''
for i in range(bytes_per_word):
word = ("%02X" % (bytes[i] if i < len(bytes) else 0)) + word
ofile.write("%s\n" % word)
ifile.close()
ofile.close()