2 ###############################################################
3 #binary-to-hex perl tool: obtained & modified on 10/05/08
4 ###############################################################
7 my ($format, $binfile, $outfile, $arr_form, $ColNum, $show_hex, $filesize);
8 $format = $ARGV[0] || usage();
9 $binfile = $ARGV[1] || usage();
10 $outfile = $ARGV[2] || $binfile.".$format";
12 $ColNum = $ARGV[4] || usage();
15 my ($orig, @converted);
17 $orig = readdata ($binfile);
19 $orig = hexdump($orig);
20 if ($show_hex) {print "Original Binary text:\n\t $orig","\n\n";}
22 if ($format eq 'a' || $format eq 'asm') {
23 @converted = convert_to_asm ($orig);
25 } elsif ($format eq 'c') {
26 @converted = convert_to_c ($orig);
29 print "Unknown format to convert!\n";
32 if ($show_hex) {print "Converted hex text:\n",join ('', @converted), "\n";}
34 writedata ($outfile, @converted);
37 my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
50 unshift (@data, "byte 0");
51 $data[-1] =~s/[,|\nbyte] 0$//g;
54 if ($i++ < $ColNum-1) {
66 my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
71 if ($i++ < $ColNum-1) {
78 unshift (@data, "unsigned char data[$filesize] = {\n\t0x"); #add some pattern at the front of @data
80 $data[-1] =~s/[ |\n\t]//g;
85 if ($i++ < $ColNum-1) {
98 #printf "dbg:file = $file\n";
99 open (BF, "$file") || die "Cannot open $file: $!"; #$! contains current value of errno
101 $filesize = (stat($file))[7];
102 my ($DATA) = ""; #<BF>;
103 my (@Data_check) = <BF>;
104 foreach $line (@Data_check){
105 #printf "dbg:line = $line\n";
108 #printf "dbg:DATA string = $DATA\n";
114 my ($file, @FomatData) = @_;
115 open (AF, ">$file") || die "Cannot open $file: $!";
121 foreach (@FomatData) {
150 join ' ', map { sprintf "%02X", $_ } unpack "C*", $_[0];
156 Usage: bin2hex format binfile outfile
157 format format to convert to,
160 binfile binary file you want to convert.
161 outfile output file to store the result of output.
162 arr_form displayed in array-form
163 ColNum num of columns of the shown array