in reply to Re: How to open a file in hexadecimal form?
in thread How to open a file in hexadecimal form?

It's a binary file that I need to edit. The following code works perfectly:

binmode STDIN;
binmode STDOUT;
$/ = \8192; # set "input record separator" to 8KB per read

while (<>) {
tr/\xa7/\x00/;
print;
}


Is it possible to look for more than one hexadecimal number back to back. For example, if I wanted to search the variable for all instances of x31 x33 x39 x37 (not appearing individually but appearing as 1397) how could I replace a set of four hexadecimal numbers with x00 x00 x00 x00 ? And I'm sure this is probably a stupid question, but why does the input record separator need to be set at 8kb? Thanks.
  • Comment on Re^2: How to open a file in hexadecimal form?

Replies are listed 'Best First'.
Re^3: How to open a file in hexadecimal form?
by Roy Johnson (Monsignor) on May 12, 2005 at 12:30 UTC
    <code></code> tags are the preferred way to post formatted plain text here.

    The input record separator needs to be set to something, and 8kb is a reasonable value. It shouldn't be "\n" because you might not have any. But you can use it for your new problem:

    $/ = '1397'; # You don't have to specify the hex numbers; it's all th +e same to Perl while (<>) { # 1397 appears at the end of the record, if at all # so an anchored search-and-replace handles it s/1397$/\0\0\0\0/; }

    Caution: Contents may have been coded under pressure.
      That's some pretty wicked Perl foo :-)

      -Scott