BEGIN { BINMODE = "r" infile = ARGV[1] ARGV[1] = "" if(length(infile)==0) { print "\n" \ "bin2c - (C) 2026 by Niccolò Izzo\n" \ " License: WTFPL (wikipedia.org/wiki/WTFPL)\n" \ "\n" \ "usage: gawk -b -f this_file input_binary_file\n" \ "\n" \ "Convert the binary data of a file into a hex form.\n" exit(3) } #################################################################################################### # From "The GNU Awk User's Guide" paragraph: "Translating Between Characters and Numbers" (adapted) #################################################################################################### for(i=0; i<256; i++) _ord_[sprintf("%c", i)] = i RS="a sequence that is SURELY not found in any input file, \x00 not even in this file" getline < infile # read the whole input file into $0 len = length($0) bytes_x_line = 12 outfile = infile gsub(getext(infile), ".h", outfile) arr_name = basename(infile) gsub("-", "_", arr_name) # Print header printf("unsigned char %s[] = {\n", arr_name) > outfile for(i=0; i outfile # printf("0x%08x ", i) for(j=i; j outfile # _ord_(char) converts a character to its ASCII code # same as 'ord(char)' in Python, or Asc(c) in Excel/VBA. } print "" > outfile } printf("};\n\n") > outfile printf("unsigned int %s_len = %d;\n", arr_name, len) > outfile # Find offset of character to patch (UHF -> VHF) patch_offset = index($0, "SA868S-UHF") + 6 printf("unsigned int %s_patch_offset = %d;\n", arr_name, patch_offset) > outfile } # Extract basename of file function basename(filename, a) { # Get file name split(filename, a, "/") filename = a[length(a)] # Get without extension split(filename, a, ".") return a[1] } # Extract file extension function getext(filename, a) { split(filename, a, ".") return "." a[length(a)] }