#Convert Hex file to bin file
# cmd module input output continous endian
# perl hex2bin.pl hex.f bin.f 0/1 0/1
# ARGV[0] ARGV[1] ARGV[2] ARGV[3]
#typical usage
# perl hex2bin.pl hex.f bin.f 0 1
#open input hex file
$file = $ARGV[0];
open(inF, $file) or dienice ("file open failed\n");
@lines = <inF>;
close(inF);
#open output bin file
open(OUTPUT, ">$ARGV[1]") or die "ERROR : Cannot open $ARGV[1].\n";
#default not continous (1:continous 0: with "\n")
$continous = $ARGV[2];
#default big endian (1: big endian 0:little endian)
$endian = $ARGV[3];
#convert hex to bin
foreach $line (@lines){
chomp $line;
@chars = split(//,$line);
foreach $char (@chars){
if ($char eq "0"){
print OUTPUT "0000";
}
elsif ($char eq "1"){
($endian == 1)
?
(print OUTPUT "0001")
:
(print OUTPUT "1000");
}
elsif ($char eq "2"){
($endian == 1)
?
(print OUTPUT "0010")
:
(print OUTPUT "0100");
}
elsif ($char eq "3"){
($endian == 1)
?
(print OUTPUT "0011")
:
(print OUTPUT "1100");
}
elsif ($char eq "4"){
($endian == 1)
?
(print OUTPUT "0100")
:
(print OUTPUT "0010");
}
elsif ($char eq "5"){
($endian == 1)
?
(print OUTPUT "0101")
:
(print OUTPUT "1010");
}
elsif ($char eq "6"){
print OUTPUT "0110";
}
elsif ($char eq "7"){
($endian == 1)
?
(print OUTPUT "0111")
:
(print OUTPUT "1110");
}
elsif ($char eq "8"){
($endian == 1)
?
(print OUTPUT "1000")
:
(print OUTPUT "0001");
}
elsif ($char eq "9"){
print OUTPUT "1001";
}
elsif (($char eq "a")|| ($char eq "A")){
($endian == 1)
?
(print OUTPUT "1010")
:
(print OUTPUT "0101");
}
elsif ($char eq "b" || $char eq "B"){
($endian == 1)
?
(print OUTPUT "1011")
:
(print OUTPUT "1101");
}
elsif ($char eq "c" || $char eq "C"){
($endian == 1)
?
(print OUTPUT "1100")
:
(print OUTPUT "0011");
}
elsif ($char eq "d" || $char eq "D"){
($endian == 1)
?
(print OUTPUT "1101")
:
(print OUTPUT "1011");
}
elsif ($char eq "e" || $char eq "E"){
($endian == 1)
?
(print OUTPUT "1110")
:
(print OUTPUT "0111");
}
elsif ($char eq "f" || $char eq "F"){
print OUTPUT "1111";
}
#other charactor will be replaced by a space
else {
print OUTPUT " ";
}
}
if ($continous == 0)
{print OUTPUT "\n"};
}
#show error message (useless)
sub dienice {
my($errmsg) = @_;
print"$errmsg\n";
exit;
}