-
Notifications
You must be signed in to change notification settings - Fork 10
/
dat2inc.c
50 lines (45 loc) · 1006 Bytes
/
dat2inc.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
45
46
47
48
49
50
//
// Datafile -> C include file
//
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *in;
FILE *out;
int length;
int c;
if (argc < 3)
{
printf("Converts a binary datafile into a C include file\nUsage: dat2inc <datafile> <outputfile>\n");
return 1;
}
in = fopen(argv[1], "rb");
if (!in)
{
printf("Datafile open error!\n");
return 1;
}
out = fopen(argv[2], "wt");
if (!out)
{
printf("Includefile open error!\n");
return 1;
}
fseek(in, 0, SEEK_END);
length = ftell(in);
fseek(in, 0, SEEK_SET);
fprintf(out, "unsigned char datafile[] = {\n");
for (c = 0; c < length; c++)
{
if (c)
{
fprintf(out, ", ");
if (!(c % 10)) fprintf(out, "\n");
}
fprintf(out, "0x%02x", fgetc(in));
}
fprintf(out, "};\n");
fclose(in);
fclose(out);
return 0;
}