-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary2c.c
44 lines (41 loc) · 876 Bytes
/
binary2c.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[]) {
FILE *src;
FILE *dst;
int i, total;
if (argc != 4) {
fprintf(stderr, "Usage: %s <name> <src> <dst>\n", argv[0]);
return 1;
}
src = fopen(argv[2], "rb");
if (!src) {
fprintf(stderr, "Cannot open: %s\n", argv[2]);
return 1;
}
dst = fopen(argv[3], "w");
if (!dst) {
fprintf(stderr, "Cannot open: %s\n", argv[3]);
return 1;
}
fprintf(dst, "#include <stdint.h>\n\n");
fprintf(dst, "uint8_t %s[] = {\n", argv[1]);
i = 0;
total = 0;
for (;;) {
int ch = fgetc(src);
if (ch < 0) { break; }
fprintf(dst, " %3d,", ch);
++i;
++total;
if (i >= 16) {
fprintf(dst, "\n");
i = 0;
}
}
fprintf(dst, "\n");
fprintf(dst, "};\n\n");
fprintf(dst, "uintptr_t %s_size = %d;\n", argv[1], total);
fclose(dst);
fclose(src);
return 0;
}