-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathbin2code.py
More file actions
31 lines (26 loc) · 811 Bytes
/
bin2code.py
File metadata and controls
31 lines (26 loc) · 811 Bytes
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
#!/usr/bin/python3
'''
# Example:
> python bin2code.py m5_logo.jpg
> Out:m5_logo.jpg.c Done!
'''
import sys, os
in_name = sys.argv[1]
out_name = in_name + '.c'
file_size = os.path.getsize(in_name)
with open(in_name, 'rb') as infile:
with open(out_name, 'wb+') as outfile:
arrary_name = 'const unsigned char ' + out_name[0:out_name.find('.')] + '[' +str(file_size)+'] = {\n'
outfile.write(arrary_name.encode('utf-8'))
while True:
data = infile.read(20)
if len(data) > 0:
# outfile.write('\t'.encode('utf-8'))
for i in range(0, len(data)):
d = "0x%02x," % data[i]
outfile.write(d.encode('utf-8'))
outfile.write('\n'.encode('utf-8'))
else:
outfile.write('};\n'.encode('utf-8'))
break
print('Out:'+ out_name +' Done!')