OSDN Git Service

new file
[stock/test.git] / bin2h
1 #!/bin/bash
2 # Copyright 2014,2015 Pawel Jewstafjew Pawel<dot>Jewstafjew<at>gmail<dot>com
3 # converts a binary file to a C header file
4
5 binfile="$1"
6 name="$2"
7 out=${name}.h.tmp
8
9 size=$(cat "$binfile" | wc --bytes)
10 echo "name=${name}, size=${size}"
11
12 echo "#ifndef _${name}_H"                       > ${out}
13 echo "#define _${name}_H"                       >> ${out}
14 echo "enum {${name}_size = ${size}};"           >> ${out}
15 echo "const unsigned char ${name}[${size}] = {" >> ${out}
16
17 for v in $(od --output-duplicates --width=1 --address=n --format=x1 "$binfile" )
18 do
19    echo "0x$v,"                                 >> ${out}
20 done
21
22 echo "};"                                       >> ${out}
23 echo "#endif // _${name}_H"                     >> ${out}
24
25 mv ${out} ${name}.h