# HG changeset patch # User Mychaela Falconia # Date 1712518863 0 # Node ID 871e83f0cb769b318cb5a4035de5fe30d2802bda # Parent db5772dac3c3c435a1349bd676b6fc579c3feab6 utils: gen-hex-c utility written diff -r db5772dac3c3 -r 871e83f0cb76 .hgignore --- a/.hgignore Sun Apr 07 19:28:20 2024 +0000 +++ b/.hgignore Sun Apr 07 19:41:03 2024 +0000 @@ -13,3 +13,5 @@ ^ringing/ringing\. ^ringing/ringing-efr\. ^ringing/ringing-fr\. + +^utils/gen-hex-c$ diff -r db5772dac3c3 -r 871e83f0cb76 Makefile --- a/Makefile Sun Apr 07 19:28:20 2024 +0000 +++ b/Makefile Sun Apr 07 19:41:03 2024 +0000 @@ -1,4 +1,4 @@ -SUBDIR= amrdiff dmw ringing +SUBDIR= amrdiff dmw ringing utils all: ${SUBDIR} diff -r db5772dac3c3 -r 871e83f0cb76 utils/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils/Makefile Sun Apr 07 19:41:03 2024 +0000 @@ -0,0 +1,11 @@ +CC= gcc +CFLAGS= -O2 +PROG= gen-hex-c + +all: ${PROG} + +${PROG}: ${PROG}.c + ${CC} ${CFLAGS} -o $@ $@.c + +clean: + rm -f *.o ${PROG} diff -r db5772dac3c3 -r 871e83f0cb76 utils/gen-hex-c.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils/gen-hex-c.c Sun Apr 07 19:41:03 2024 +0000 @@ -0,0 +1,45 @@ +/* + * This program reads an arbitrary binary file and converts it into + * ASCII hex output of the form '0xXX,0xXX,...' intended for inclusion + * into C sources as a const char array. + */ + +#include +#include + +main(argc, argv) + char **argv; +{ + FILE *inf, *outf; + int c, lcnt; + + if (argc != 3) { + fprintf(stderr, "usage: %s input.bin output.c\n", argv[0]); + exit(1); + } + inf = fopen(argv[1], "r"); + if (!inf) { + perror(argv[1]); + exit(1); + } + outf = fopen(argv[2], "w"); + if (!outf) { + perror(argv[2]); + exit(1); + } + lcnt = 0; + for (;;) { + c = getc(inf); + if (c < 0) + break; + fprintf(outf, "0x%02X,", c); + lcnt++; + if (lcnt >= 16) { + putc('\n', outf); + lcnt = 0; + } + } + if (lcnt) + putc('\n', outf); + exit(0); +}