bin2c.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* bin2c.c -- dump binary file in hex format
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "binary-io.h"
  19. int
  20. main (int argc, char *argv[])
  21. {
  22. int c;
  23. int i;
  24. if (argc != 1)
  25. {
  26. int ishelp = 0;
  27. FILE *stream;
  28. if (argc == 2 && argv[1][0] == '-')
  29. {
  30. const char *opt = &argv[1][1];
  31. if (*opt == '-')
  32. ++opt;
  33. ishelp = *opt == 'h' || *opt == 'H';
  34. }
  35. stream = ishelp ? stdout : stderr;
  36. fprintf (stream, "Usage: %s < input_file > output_file\n", argv[0]);
  37. fprintf (stream, "Prints bytes from stdin in hex format.\n");
  38. exit (!ishelp);
  39. }
  40. SET_BINARY (fileno (stdin));
  41. i = 0;
  42. while ((c = getc (stdin)) != EOF)
  43. {
  44. printf ("0x%02x,", c);
  45. if (++i == 16)
  46. {
  47. printf ("\n");
  48. i = 0;
  49. }
  50. }
  51. if (i != 0)
  52. printf ("\n");
  53. exit (0);
  54. }