hex2binary script
Published by Alphamonk on 2009/3/18 (998 reads)
I am really not sure when I wrote this. If you are looking for script to change a file full of hex data to another file full of binary data this script will do it.
I obviously did not find a formula to use for the conversion, so I am using a matrix. I would prefer using a formula. I don't feel like looking for one now, If you know a formula for converting hex to binary email me and I will incorporate it into this script.
The script works just fine the way it is.
I obviously did not find a formula to use for the conversion, so I am using a matrix. I would prefer using a formula. I don't feel like looking for one now, If you know a formula for converting hex to binary email me and I will incorporate it into this script.
The script works just fine the way it is.
use strict;
use warnings;
use Getopt::Long;
my $ver = "1.0";
my (
$char,@chars,$line,@lines,$continous,
);
my (
$hex,$bin,$endian,$version,
) = Arguments();
if($version){
print "Version is $vern";
exit;
}
#open output bin file
open(BINFILE, ">$bin") or die "ERROR: Cannot open $bin filen";
#open input hex file
open(HEXFILE, $hex) or die "ERROR: Can not open $hex filen";
while(<HEXFILE>){
$line = $_;
#convert hex to bin
chomp $line;
@chars = split(//,$line);
foreach $char (@chars){
if ($char eq "0"){
print BINFILE "0000";
}
elsif ($char eq "1"){
($endian == 1)
?
(print BINFILE "0001")
:
(print BINFILE "1000");
}
elsif ($char eq "2"){
($endian == 1)
?
(print BINFILE "0010")
:
(print BINFILE "0100");
}
elsif ($char eq "3"){
($endian == 1)
?
(print BINFILE "0011")
:
(print BINFILE "1100");
}
elsif ($char eq "4"){
($endian == 1)
?
(print BINFILE "0100")
:
(print BINFILE "0010");
}
elsif ($char eq "5"){
($endian == 1)
?
(print BINFILE "0101")
:
(print BINFILE "1010");
}
elsif ($char eq "6"){
print BINFILE "0110";
}
elsif ($char eq "7"){
($endian == 1)
?
(print BINFILE "0111")
:
(print BINFILE "1110");
}
elsif ($char eq "8"){
($endian == 1)
?
(print BINFILE "1000")
:
(print BINFILE "0001");
}
elsif ($char eq "9"){
print BINFILE "1001";
}
elsif (($char eq "a")|| ($char eq "A")){
($endian == 1)
?
(print BINFILE "1010")
:
(print BINFILE "0101");
}
elsif ($char eq "b" || $char eq "B"){
($endian == 1)
?
(print BINFILE "1011")
:
(print BINFILE "1101");
}
elsif ($char eq "c" || $char eq "C"){
($endian == 1)
?
(print BINFILE "1100")
:
(print BINFILE "0011");
}
elsif ($char eq "d" || $char eq "D"){
($endian == 1)
?
(print BINFILE "1101")
:
(print BINFILE "1011");
}
elsif ($char eq "e" || $char eq "E"){
($endian == 1)
?
(print BINFILE "1110")
:
(print BINFILE "0111");
}
elsif ($char eq "f" || $char eq "F"){
print BINFILE "1111";
}
#other charactor will be replaced by a space
else {
print BINFILE " ";
}
}
print BINFILE "n";
}
close(HEXFILE);
close(BINFILE);
sub Arguments {
my (
$hex,$bin, $endian, $version, $help,
);
GetOptions(
"hexfile=s" => $hex,
"binfile=s" => $bin,
"endian=s" => $endian,
"version" => $version,
"help" => $help,
);
Usage() unless ($hex && $bin) or $version;
Usage() if ($help);
if($endian){
Usage() unless (($endian eq "l") or ($endian eq "b"));
}
if($endian){
if($endian eq "l"){
$endian = 0;
} else {
$endian = 1;
}
} else {
$endian = 1;
}
return (
$hex,$bin,$endian, $version,
);
}
sub Usage {
my $usageoutput = <<USAGE;
Syntax: <hex2bin.pl> [options]
--hexfile : hexfile file name
--binfile : binfile file name
--endian : arguments are b and l only default is b
--version : Print version number
--help : Print usage information
NOTE: : Conversion goes in one direction only hex to binary
Example : <hex2bin.pl> --hexfile myinputfile --binfile myoutputfile
: <hex2bin.pl> --hexfile myinputfile --binfile myoutputfile --endian b
: <hex2bin.pl> --hexfile myinputfile --binfile myoutputfile --endian l
USAGE
print "$usageoutput";
exit;
}
| Navigate through the articles | |
Mailing from Perl
|
Perl Automation Script using a config file
|
Voters total: 0
Average: 0
|
The comments are owned by the poster. We aren't responsible for their content.
|


