comparison utils/gen-hex-c.c @ 13:871e83f0cb76

utils: gen-hex-c utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Apr 2024 19:41:03 +0000
parents
children
comparison
equal deleted inserted replaced
12:db5772dac3c3 13:871e83f0cb76
1 /*
2 * This program reads an arbitrary binary file and converts it into
3 * ASCII hex output of the form '0xXX,0xXX,...' intended for inclusion
4 * into C sources as a const char array.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 main(argc, argv)
11 char **argv;
12 {
13 FILE *inf, *outf;
14 int c, lcnt;
15
16 if (argc != 3) {
17 fprintf(stderr, "usage: %s input.bin output.c\n", argv[0]);
18 exit(1);
19 }
20 inf = fopen(argv[1], "r");
21 if (!inf) {
22 perror(argv[1]);
23 exit(1);
24 }
25 outf = fopen(argv[2], "w");
26 if (!outf) {
27 perror(argv[2]);
28 exit(1);
29 }
30 lcnt = 0;
31 for (;;) {
32 c = getc(inf);
33 if (c < 0)
34 break;
35 fprintf(outf, "0x%02X,", c);
36 lcnt++;
37 if (lcnt >= 16) {
38 putc('\n', outf);
39 lcnt = 0;
40 }
41 }
42 if (lcnt)
43 putc('\n', outf);
44 exit(0);
45 }