-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin2string.pl
executable file
·42 lines (36 loc) · 996 Bytes
/
bin2string.pl
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
41
42
#!/usr/bin/perl
#
# bin2string.pl -- encode a binary file as a C string
#
use strict;
use Carp;
use Getopt::Long;
use constant false => 0;
use constant true => 1;
my %option = ();
GetOptions(\%option,'name=s', 'source=s');
die "$0: no variable name defined" unless defined $option{name};
die "$0: no source file defined" unless defined $option{source};
open(FH, "<$option{source}") or die "$0: could not read `$option{source}'";
binmode(FH);
binmode(STDOUT);
my $bin_size = (stat(FH))[7];
my $buff;
my $buff_size;
my $first_time = true;
print "const unsigned char " . $option{name} . "[" . $bin_size. "] = { \n";
while (($buff_size = read(FH, $buff, 8)) != 0) {
if (!$first_time) {
print ",\n";
} else {
$first_time = false;
}
print " ";
my @data = unpack("C*", $buff);
for (my $i = 0; $i < @data; ++$i) {
printf("'\\x%02x'%s", int($data[$i]), ($i < ($buff_size - 1)) ? ",": "");
}
}
print "\n};\n";
close(FH);
# End of bin2string.pl