Skip to content

Instantly share code, notes, and snippets.

@hikiko
Last active November 11, 2018 10:49
Embed
What would you like to do?
creates unsigned char array from bin data
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
int cur, c, prevc;
if(argc < 2) {
fprintf(stderr, "You should provide a filename.\n");
return 1;
}
if(!(fp = fopen(argv[1], "rb"))) {
fprintf(stderr, "Failed to open file %s\n", argv[1]);
return 1;
}
printf("unsigned char data[] = {\n\t");
cur = 8;
if((prevc = fgetc(fp)) == -1) {
fprintf(stderr, "File %s is empty\n", argv[1]);
return 1;
}
while((c = fgetc(fp)) != -1) {
cur += printf("%u,", (unsigned int)prevc);
if(cur >= 72) {
printf("\n\t");
cur = 8;
} else {
putchar(' ');
cur++;
}
prevc = c;
}
printf("%u\n};\n", prevc);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment