binmode is only used for encoding purposes, in case of ASCII text file (i.e. non-UTF8 or similar) it mostly only used to make sure that the line endings a not converted from Windows (CRLF) to Unix (LF) or Mac (CR) format or the other way around, which would corrupt a "binary" file. (Note: Actually all files are "binary", ASCII text files just have a simple "binary" format: 1 byte => 1 letter.) AFIK under Unix/Linux binmode doesn't normally do anything, it's added to make the script working prober Windows.
You can convert binary strings to hexadezimal representation (i.e. text strings) and back using the Perl functions pack() and unpack(). There are a little confusing - I personally have problems to remember which does which way conversation - but very powerful. See perldoc for the details and you will definitely find a tutorial on the web how to use it.
The code should look like this (untested):
open(IN, '<', $ifilename);
binmode(IN);
while (!eof IN) {
read(IN, my $binstring, $length);
my $hexstring = unpack("...", $binstring);
}
[...]
my $newbin = pack("...", $hexstring);
Please note that pack() awaits a list of arguments...
Update:
See this tutorial. Direct at the start it shows how to do bin2hex and hex2bin! |