view utils/gen-hex-lines.c @ 74:e78c6b1ecb91

trau-decode: refactor trau-hr-dump The desire is to create a companion program that will read hex lines representing TRAU-8k frames and then decode those frames in exactly the same way how we currently decode frames read from binary capture files. Hence the decoder portion of trau-hr-dump needs to be factored out.
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Feb 2025 03:00:31 +0000
parents ffbbce856ac7
children
line wrap: on
line source

/*
 * This program reads an arbitrary binary file and converts it into ASCII hex
 * output with a specified number of bytes per line.
 */

#include <stdio.h>
#include <stdlib.h>

main(argc, argv)
	char **argv;
{
	FILE *inf, *outf;
	int c, lcnt, bpl;

	if (argc != 4) {
		fprintf(stderr,
			"usage: %s input.bin output.hex bytes-per-line\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);
	}
	bpl = atoi(argv[3]);
	lcnt = 0;
	for (;;) {
		c = getc(inf);
		if (c < 0)
			break;
		fprintf(outf, "%02X", c);
		lcnt++;
		if (lcnt >= bpl) {
			putc('\n', outf);
			lcnt = 0;
		}
	}
	if (lcnt)
		putc('\n', outf);
	exit(0);
}