3 #define _CRT_SECURE_NO_WARNINGS
11 int main (int argc
, char *argv
[])
23 puts("Usage: bin2h [input] [output] [variable]");
28 output_name
= argv
[2];
29 variable_name
= argv
[3];
31 input_file
= fopen(input_name
, "rb");
33 if (NULL
== input_file
)
35 printf("Could not open input file '%s': %s\n", input_name
, strerror(errno
));
39 output_file
= fopen(output_name
, "w");
41 if (NULL
== output_file
)
43 printf("Could not open output file '%s': %s\n", output_name
, strerror(errno
));
47 if (fprintf(output_file
, "static const unsigned char %s[] = {", variable_name
) < 0)
49 printf("Could not write to output file '%s': %s\n", output_name
, strerror(ferror(output_file
)));
53 while (0 == feof(input_file
))
55 unsigned char buffer
[4096];
56 size_t i
, count
= fread(buffer
, 1, sizeof(buffer
), input_file
);
58 if (sizeof(buffer
) != count
)
60 if (0 == feof(input_file
) || 0 != ferror(input_file
))
62 printf("Could not read from input file '%s': %s\n", input_name
, strerror(ferror(input_file
)));
67 for (i
= 0; i
< count
; ++i
)
71 if (fprintf(output_file
, "\n ") < 0)
73 printf("Could not write to output file '%s': %s\n", output_name
, strerror(ferror(output_file
)));
78 if (fprintf(output_file
, "0x%2.2x, ", buffer
[i
]) < 0)
80 printf("Could not write to output file '%s': %s\n", output_name
, strerror(ferror(output_file
)));
87 if (fprintf(output_file
, "\n};\n") < 0)
89 printf("Could not write to output file '%s': %s\n", output_name
, strerror(ferror(output_file
)));
93 if (fclose(output_file
) < 0)
95 printf("Could not close output file '%s': %s\n", output_name
, strerror(ferror(output_file
)));