Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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 :(

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-06-24 23:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.