-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2c.c
43 lines (39 loc) · 986 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
/***************************
* File: bin2c.c
* Author: Guo Xiaoyong
* Date: DEC 19, 2011
***************************/
#include <assert.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp;
char *filename = NULL;
char *varname = NULL;
int letter;
size_t size;
if (argc < 3) {
fprintf(stderr, "too few input parameters!\n\n");
printf("bin2c: convert a binary file to a large C array\n"
"USAGE:\n"
" bin2c file_name variable_name\n\n");
return 1;
}
filename = argv[1];
varname = argv[2];
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "bin2c error: cannot open the file %s\n", filename);
return 2;
}
size = 0;
printf("unsigned char %s[] = { ", varname);
while ((letter = fgetc(fp)) != EOF) {
assert(letter >= 0 && letter <= 255);
printf("0x%02X,\n", letter);
size++;
}
printf("};\n\n");
printf("/* The size of the array is %lu. */", size);
fclose(fp);
return 0;
}