I have been trying to convert a binary file to hex representation, and then taking that hex representation and recreating the binary file. I have been able to read in a binary file as hex using the following code
my $buffer;
open INFILE, "test.bin" or die "Can't open test.bin for reading: $!\n"
+;
open OUTFILE, ">test.bin" or die "Can't open test.bin for writing: $!\
+n";
select OUTFILE;
binmode INFILE;
while (read(INFILE, $buffer, 1) and printf("%X", ord($buffer))){};
print "\n";
But I am having trouble taking this hex representation and recreating the binary file with it. Here is the script I have currently that is not working
my $buffer;
open INFILE, "test.text" or die "Can't open test.txt for reading: $!\n
+";
open OUTFILE, ">test_updated.bin" or die "Can't open test_updated.bin
+for writing: $!\n";
binmode OUTFILE;
my $line = <INFILE>;
chomp $line;
my @list = split //, $line;
my $hex_value;
my $i =1;
foreach my $character (@list)
{
if($i%2 == 0)
{
$hex_value .= $character;
syswrite OUTFILE, hex($hex_value);
}
elsif($i%2 == 1)
{
$hex_value = "0x".$character;
}
$i++;
}
This code just write a bunch of number to the binary file which is not what I want. Anyone have any ideas on what I am doing wrong?