Skip to content

Instantly share code, notes, and snippets.

@henkman
Created December 7, 2023 23:10
Show Gist options
  • Save henkman/1021f1b1371e73dc7576bf9c15551182 to your computer and use it in GitHub Desktop.
Save henkman/1021f1b1371e73dc7576bf9c15551182 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: binc [input]\n");
return 0;
}
int fd = open(argv[1], O_RDONLY);
if (!fd) {
return 1;
}
struct stat statbuf;
fstat(fd, &statbuf);
printf("uint8_t bin[%ld] = {", statbuf.st_size);
uint8_t buf[8 * 1024];
size_t n;
int first = 1;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
for (size_t i = 0; i < n; i++) {
if (!first)
putchar(',');
else
first = 0;
printf("0x%02X", buf[i]);
}
}
printf("};");
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment