Skip to content

Instantly share code, notes, and snippets.

@me-no-dev
Created September 23, 2016 10:02
Show Gist options
  • Select an option

  • Save me-no-dev/f137a950ce6dedb641d427d8db6355d2 to your computer and use it in GitHub Desktop.

Select an option

Save me-no-dev/f137a950ce6dedb641d427d8db6355d2 to your computer and use it in GitHub Desktop.

Revisions

  1. me-no-dev created this gist Sep 23, 2016.
    51 changes: 51 additions & 0 deletions filetoarray.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
    #include <string.h>

    int main(int argc, char *argv[]){
    FILE * fp;
    char *buffer;
    long flen;
    char *fname;
    char pname[1024];

    if( argc == 2 ) {
    fname = argv[1];
    strcpy(pname, fname);
    char * dot = strchr(pname, '.');
    while(dot != NULL){
    *dot = '_';
    dot = strchr(pname, '.');
    }
    }else {
    printf("Filename not supplied\n");
    return 1;
    }

    fp = fopen(fname, "rb");
    fseek(fp, 0, SEEK_END);
    flen = ftell(fp);
    rewind(fp);

    buffer = (char *)malloc((flen+1)*sizeof(char));
    fread(buffer, flen, 1, fp);
    fclose(fp);

    printf("\n//File: %s, Size: %lu\n",fname, flen);
    printf("#define %s_len %lu\n", pname, flen);
    printf("const uint8_t %s[] PROGMEM = {\n", pname);
    long i;
    for(i=0;i<flen;i++){
    printf(" 0x%02X", (uint8_t)buffer[i]);
    if(i<(flen - 1)){
    printf(",");
    }
    if((i%16)==15){
    printf("\n");
    }
    }
    printf("\n};\n\n");
    free(buffer);
    return 0;
    }