txt2c.c 564 Bytes
Newer Older
1 2 3 4
#include <stdio.h>

#define per_line 12

5 6
void
txt2c(char *sym, FILE *fp)
7
{
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
	unsigned char buf[per_line];
	int i;
	int sz = 0;

	printf("unsigned char %s[] = {\n", sym);
	while (1) {
		sz = fread(buf, sizeof(unsigned char), per_line, fp);
		if (sz == 0)
			break;
		printf("\t");
		for (i = 0; i < sz; i++)
			printf("0x%02x, ", buf[i]);
		printf("\n");
	}

	printf("\t0x00\n};\n");
24 25 26
}


27 28
int
main(int argc, char *argv[])
29
{
30 31 32 33
	if (argc != 2) {
		fprintf(stderr, "usage: %s symbol <textfile >textfile.c\n", argv[0]);
		return 2;
	}
34

35 36
	txt2c(argv[1], stdin);
	return 0;
37
}