Skip to content

Instantly share code, notes, and snippets.

@albertz
Created January 2, 2012 16:29

Revisions

  1. albertz created this gist Jan 2, 2012.
    19 changes: 19 additions & 0 deletions bin2c.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #include <stdio.h>
    #include <assert.h>

    int main(int argc, char** argv) {
    assert(argc == 2);
    char* fn = argv[1];
    FILE* f = fopen(fn, "r");
    printf("char a[] = {\n");
    unsigned long n = 0;
    while(!feof(f)) {
    unsigned char c;
    if(fread(&c, 1, 1, f) == 0) break;
    printf("0x%.2X,", (int)c);
    ++n;
    if(n % 10 == 0) printf("\n");
    }
    fclose(f);
    printf("};\n");
    }