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?

In reply to Recreating a binary file from hex representation by choedebeck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.