Atlas - xxd.py
Home / ext / SDL / cmake Lines: 7 | Size: 1481 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1#!/usr/bin/env python 2 3import argparse 4import os 5import pathlib 6import re 7 8def main(): 9 parser = argparse.ArgumentParser(allow_abbrev=False, description="Convert file into includable C header") 10 parser.add_argument("--in", "-i", type=pathlib.Path, metavar="INPUT", dest="input", required=True, help="Input file") 11 parser.add_argument("--out", "-o", type=pathlib.Path, metavar="OUTPUT", dest="output", required=True, help="Output header") 12 parser.add_argument("--columns", type=int, default=12, help="Column count") 13 args = parser.parse_args() 14 15 t = pathlib.Path() 16 varname, _ = re.subn("[^a-zA-Z0-9]", "_", str(args.input.name)) 17 18 binary_data = args.input.open("rb").read() 19 20 with args.output.open("w", newline="\n") as fout: 21 fout.write("unsigned char {}[] = {{\n".format(varname)) 22 bytes_written = 0 23 while bytes_written < len(binary_data): 24 col = bytes_written % args.columns 25 if col == 0: 26 fout.write(" ") 27 column_data = binary_data[bytes_written:bytes_written+args.columns] 28 fout.write(", ".join("0x{:02x}".format(d) for d in column_data)) 29 bytes_written += len(column_data) 30 if bytes_written < len(binary_data): 31 fout.write(",\n") 32 else: 33 fout.write("\n") 34 fout.write("}};\nunsigned int {}_len = {:d};\n".format(varname, len(binary_data))) 35 36if __name__ == "__main__": 37 raise SystemExit(main()) 38[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.