bin2header.c 792 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* bin2header.c - program to convert binary file into a C structure
  2. * definition to be included in a header file.
  3. *
  4. * (C) Copyright 2008 by Harald Welte <laforge@openmoko.org>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. int main(int argc, char **argv)
  11. {
  12. if (argc < 2) {
  13. fprintf(stderr, "%s needs one argument: the structure name\n",
  14. argv[0]);
  15. exit(1);
  16. }
  17. printf("/* bin2header output - automatically generated */\n");
  18. printf("unsigned char %s[] = {\n", argv[1]);
  19. while (1) {
  20. int i, nread;
  21. unsigned char buf[10];
  22. nread = read(0, buf, sizeof(buf));
  23. if (nread <= 0)
  24. break;
  25. printf("\t");
  26. for (i = 0; i < nread - 1; i++)
  27. printf("0x%02x, ", buf[i]);
  28. printf("0x%02x,\n", buf[nread-1]);
  29. }
  30. printf("};\n");
  31. exit(0);
  32. }