Skip to content

Instantly share code, notes, and snippets.

@vext01
Created July 9, 2011 15:34
Show Gist options
  • Save vext01/1073672 to your computer and use it in GitHub Desktop.
Save vext01/1073672 to your computer and use it in GitHub Desktop.
Dump a file to a C hex array
#include <stdio.h>
int
main(void)
{
FILE *f, *out;
uint8_t buf;
unsigned long i = 0;
if ((f = fopen("silence.ogg", "r")) == NULL)
perror("failed to open");
if ((out = fopen("array.c", "w")) == NULL)
perror("failed to open");
fprintf(out, "unit8_t silence[] = { ");
while (fread(&buf, 1, 1, f)) {
if (i++ % 10 == 0) {
if (fseek(out, ftell(out)-1, SEEK_SET) != 0)
perror("seek");
fprintf(out, "\n\t");
}
fprintf(out, "0x%02x, ", buf);
}
if (fseek(out, ftell(out)-2, SEEK_SET) != 0)
perror("seek");
fprintf(out, "\n};");
fclose(f);
fclose(out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment