Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Searching a file for an undetermined hexadecimal string

by Anonymous Monk
on May 13, 2005 at 03:25 UTC ( #456593=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to be able to open a file in binary mode, then search for a specific string of hexadecimal numbers and replace each character with the hexadecimal character 00. The problem I have is that the string I'm searching for will vary in length and content. Each string will start with the same characters (31,33,39,37) and end with the same character (00). However, each string will contain different characters between the beginning (31,33,39,37) and end (00) as well as being different lengths. This is what the code looks like right now:
binmode STDIN; binmode STDOUT; $/ = '1397'; while (<>) { s/1397$/\0\0\0\0/; print; }
How can I tell the program to start replacing characters when it encounters 1397 and then stop replacing characters when it encounters a 0? Is there some sort of wildcard character that I can use that will replace every character it encounters?


Replies are listed 'Best First'.
Re: Searching a file for an undetermined hexadecimal string
by ChemBoy (Priest) on May 13, 2005 at 04:32 UTC

    The line-processing approach above unfortunately does almost exactly the opposite of what you want, unless I'm tireder than I thought. The problem you're going to have is that you aren't (or at least, you haven't mentioned that you are) reading a record-based file, so no matter what size chunk you read in, unless you read the whole file into a scalar, you run the risk that you'll have a string that begins in one read and ends in another. There are a few ways to deal with this, but for the moment I'm going to pretend you can read the whole file in as a single string, because it will make my life easier. In that case, this should do roughly what you want (and precisely what you specified):

    undef $/; $_ = <>; s/(\x31\x33\x39\x37[^\x00]*\x00)/"\x00" x length $1/ge; print;

    There's almost certainly a better way to do that regex, but I'm reasonably sure that this one will in fact work, however inelegantly.

    If you can't process the file all at once, then you will have to get creative. I'd think this would work:

    $/ = \4096; my $flag = 0; while (<>) { if ($flag) { # ended previous chunk in mid-substitution s/(^[^\x00]*(\z|\x00))/"\x00" x length $1/ge; } s/(1379[^\x00]*(\z|\x00))/"\x00" x length $1/ge; $flag = length $2 ? 0 : 1; # this could be readily golfed, I real +ize }

    Note that it is late and I'm not testing this code, so it is by no means 100% guaranteed not to sneeze demons at you. That said, it really ought to work fine. :-)

    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      Problem solved. Thanks for your help.
Re: Searching a file for an undetermined hexadecimal string
by ikegami (Pope) on May 13, 2005 at 04:28 UTC
    binmode STDIN; binmode STDOUT; # Could read in the entire file, # but read in chunks ending with # 00h to save memory. $/ = "\x00"; while (<>) { s/(\x31\x33\x39\x37.*?\x00)/"\x00" x length($1)/e; # Or the following if you don't want # to replace the start and the end: #s/(\x31\x33\x39\x37)(.*?)(\x00)/ # $1 . ("\x00" x length($2)) . $3 #/e; print; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://456593]
Approved by ChemBoy
and God said, "Let Newton be!"...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-09-23 10:31 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (272 votes). Check out past polls.