Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Binary buffer - substr

by highland7 (Novice)
on Jan 13, 2013 at 22:29 UTC ( #1013138=perlquestion: print w/ replies, xml ) Need Help??
highland7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks

I read/create IP packets using Net::Frame::Layer.
I already have raw packet dump in $packet_dump.
I need to search for 4 consecutive bytes with values 0x16 0x3 0x1 0x1 and make a new binary buffer which will be send.
That new binary buffer will start from that 4 bytes (to the end of $packet_dump).
How can i do it ?
I have tried it with qq, pack, unpack but without suceess :(

Comment on Binary buffer - substr
Replies are listed 'Best First'.
Re: Binary buffer - substr
by dave_the_m (Prior) on Jan 13, 2013 at 22:53 UTC
    (my $out_packet = $packet_dump) =~ s/^.*?(?=\x16\x03\x01\x01)//s;
    edit: added /s modifier as pointed out by 7stud


      Thanks, it's working !!!
      One more question: How can i find index number for specific binary string ? I would like to find out from which byte that specific 4 bytes appear (/x16/x03/0x1/0x0) Thanks
        index($packet_dump, "\x16\x03\x01\x01")


Re: Binary buffer - substr
by 7stud (Deacon) on Jan 14, 2013 at 06:21 UTC
    Binary data can contain newline characters, which cannot be matched by the default dot in a regex.
    use strict; use warnings; use 5.012; my $str = "\x16-\x03\x01\x01-\x16\x03\x01\x01PAY\nLOAD"; if ($str =~ /(\x16\x03\x01\x01 .*)/xms) { say $1; printf "%vd \n", $1; say length($str) - length($1); } --output:-- PAY LOAD 6
      Binary data can contain newline characters, which cannot be matched by the default dot in a regex
      Good point. I've updated my original post to include /s


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013138]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-11-30 13:26 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (770 votes), past polls