bin2hex.pl (1403B)
1 #!/usr/bin/perl 2 # 3 # 4 # This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 5 # 6 # Copyright 2007-2019 Broadcom Inc. All rights reserved. 7 # 8 # bin2hex.pl 9 # Convert Binary file to ASCII/HEX 10 # 11 12 ($#ARGV == 1) || 13 die "Usage: $0 <binary-file> <Ascii_Hex-file>\n" . 14 "\tThis utility converts Binary in <binary-file>\n" . 15 "\tand converts to Ascii/Hex and writes them into <Ascii_Hex-file>.\n"; 16 17 my $in_file=shift; 18 my $out_file=shift; 19 20 print STDOUT "Converting ", $in_file, " to ", $out_file, " ..."; 21 22 $RECSIZE = 1; # size of record, in bytes 23 open(FH, "<$in_file") || die "can't read $in_file : $!"; 24 open(FHO, ">$out_file") || die "can't write $out_file : $!"; 25 26 binmode FH; 27 28 $count = 0; 29 while ( ! eof(FH) ) { 30 if (read(FH, $byte0, $RECSIZE) != $RECSIZE) { 31 $byte0 = "\000"; 32 } 33 34 $byte0 = ord($byte0); 35 if ($count == 0) { 36 print FHO sprintf(" 0x%02x", $byte0); 37 $count = 2; 38 } else { 39 if ($count == 16) { 40 print FHO sprintf(", 0x%02x\n", $byte0); 41 $count = 1; 42 } else { 43 print FHO sprintf(", 0x%02x", $byte0); 44 $count++; 45 } 46 } 47 } 48 close FH; 49 close FHO; 50 51 print STDOUT " done\n";