16 from argparse
import ArgumentParser
20 parser = ArgumentParser()
22 parser.add_argument(
'--binary_file_path',
25 help=
'the path to the binary file',
28 parser.add_argument(
'--array_file_path',
29 metavar=
'OUTPUT_PATH',
31 default=os.path.join(os.path.dirname(__file__),
"c_array.txt"),
32 help=
'the path to the output text file')
34 args = parser.parse_args()
36 if not os.path.exists(args.binary_file_path):
37 print(
"[ERROR] Please enter a valid file as the input!")
43 with open(args.binary_file_path,
'rb')
as f:
47 array = [
'0x%s' % z.hex()
for z
in struct.unpack(
'%dc' % len(array), array)]
52 if len(row) >= line_width:
53 row = row.rsplit(
', ', maxsplit=1)[0] +
','
55 row =
' ' * indent + last_x
57 if row !=
' ' * indent:
63 if row !=
' ' * indent:
67 with open(args.array_file_path,
'w')
as f_out:
68 f_out.write(
'\n'.join(res))
71 if __name__ ==
'__main__':