-
Notifications
You must be signed in to change notification settings - Fork 46
/
bin2c.c
44 lines (37 loc) · 1005 Bytes
/
bin2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
int main(int argc, char ** argv) {
if (argc != 4) {
printf("usage: %s input output label\n", argv[0]);
return -1;
}
FILE * in = fopen(argv[1], "rb");
if (!in) {
printf("Unable to open input file %s\n", argv[1]);
return -1;
}
FILE * out = fopen(argv[2], "wb");
if (!out) {
printf("Unable to open output file %s\n", argv[2]);
fclose(in);
return -1;
}
fseek(in, 0, SEEK_END);
int size = ftell(in);
fseek(in, 0, SEEK_SET);
fprintf(out, "static unsigned char %s[%i] = {\n ", argv[3], size);
for (unsigned i = 0; i < size; i++) {
unsigned c = fgetc(in);
fprintf(out, "0x%02x, ", c);
if (((i + 1) % 16) == 0) {
fprintf(out, "\n");
if (i != (size - 1)) fprintf(out, " ");
}
}
if ((size % 16) != 0) {
fprintf(out, "\n");
}
fprintf(out, "};\n");
fclose(in);
fclose(out);
return 0;
}