File: hex-ify.c

package info (click to toggle)
darkstat 3.0.722-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: ansic: 7,585; sh: 660; javascript: 213; makefile: 174; php: 15
file content (40 lines) | stat: -rw-r--r-- 838 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Convert a binary file to a const char array of hex. */
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
  int c;
  if (argc != 2) {
    fprintf(stderr, "usage: %s name <infile >outfile.h\n",
      argv[0]);
    exit(EXIT_FAILURE);
  }
  printf("/* this file was automatically generated with hex-ify */\n"
         "static const unsigned char %s[] = {\n", argv[1]);
  int start_of_line = 1;
  int first = 1;
  int bytes = 0;
  while ((c = getchar()) != EOF) {
    if (start_of_line) {
      printf("  ");
      start_of_line = 0;
    }
    if (first) {
      first = 0;
    } else {
      printf(", ");
    }
    printf("0x%02x", (unsigned char)c);
    bytes++;
    if (bytes == 12) {
      printf(",\n");
      first = 1;
      start_of_line = 1;
      bytes = 0;
    }
  }
  printf("\n};\n");
  return (0);
}