Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Created January 4, 2012 01:23
Show Gist options
  • Save gcmurphy/1557942 to your computer and use it in GitHub Desktop.
Save gcmurphy/1557942 to your computer and use it in GitHub Desktop.
Raw data to unsigned char C array
/* Dump raw data to a unsigned char array. */
void c_array_dump(FILE *stream, const char *label, unsigned char *data, size_t size)
{
int pos;
fprintf(stream, "const unsigned char %s[] = {\n\t", label);
for (pos = 0; pos < size ; ++pos) {
fprintf(stream, "0x%02X", (unsigned char) data[pos]);
if (pos+1 != size)
fprintf(stream, ", ");
if ((pos+1) % 8 == 0)
fprintf(stream, "\n\t");
}
fprintf(stream, "\n}; // %s\n", label);
fprintf(stream, "size_t %s_size = %ld;\n\n", label, size);
}
@mochenski
Copy link

legal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment