#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2024/02/04 01:28:06 +0900> """ bin2bas.py ... convert binary file to FreeBASIC basic script Usage : python bin2bas.py INPUT_FILE Windows10 x64 22H2 + Python 3.10.10 64bit by mieki256 License : CC0 / Public Domain """ import struct import argparse def main(): desc = "Convert binary file to basic source" parser = argparse.ArgumentParser(description=desc) parser.add_argument("-i", "--input", help="Input binary file", required=True) args = parser.parse_args() data = [] with open(args.input, "rb") as f: while True: b = f.read(1) if b: data.append(struct.unpack("B", b)[0]) else: break dataname = args.input.replace(".", "_") dataname += "_data" lengthname = dataname + "_length" print("' input file = %s" % (args.input)) print("") print("Dim As Integer %s = %d" % (lengthname, len(data))) print("Dim shared %s(0 To (%s - 1)) As ubyte" % (dataname, lengthname)) print("") print("For i As Integer = 0 To (%s - 1)" % (lengthname)) print(" read %s(i)" % (dataname)) print("Next i") print("") print("DATA _") s = "" cnt = 0 for i in range(len(data)): s += "&H%02x," % data[i] cnt += 1 if cnt % 16 == 0: if cnt == len(data): print(s[:-1]) else: print("%s _" % s) s = "" if s != "": print(s[:-1]) if __name__ == "__main__": main()