bin2uchar.c - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
bin2uchar.c (533B)
---
1 /* convert binary to unsigned char array for C */
2 #include <stdio.h>
3
4 int
5 main(int argc, char *argv[])
6 {
7 int c, i, n = 0;
8
9 i = printf("const unsigned char %s[] = {", argc > 1 && argv[1][0] ? argv[1] : "data");
10 while ((c = getchar()) != EOF) {
11 if (n) {
12 putchar(',');
13 i++;
14 } else {
15 n = 1;
16 }
17 i += printf("%d", c);
18 if (i >= 78) {
19 i = n = 0;
20 fputs(",\n", stdout); /* wrap */
21 }
22 }
23 fputs("};\n", stdout);
24
25 if (ferror(stdin) || fflush(stdout) || ferror(stdout)) {
26 perror(NULL);
27 return 1;
28 }
29
30 return 0;
31 }