Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The script itself contains something like this:NumberOfStrings (1 byte) Str1Length (1 byte) Str1Characters (Str1Length bytes) Str2Length (1 byte) Str2Characters (Str2Length bytes) ... StrNLength (1 byte) StrNCharacters (StrNLength bytes)
My question is - how can avoid the constant substr calls to remove the bytes I have already read?. I know I could use the "C/A*" template to read the length and string in one go, but that doesn't handle the variable number of strings.# Create an example byte-sequence $count = 3; $str1 = "First string"; $str2 = "Second string"; $str3 = "Third string"; $bytes = pack("C C A* C A* C A*", $count, length($str1), $str1, length($str2), $str2, length($str3), $str3); unpack("C", $count); $bytes = substr($bytes, 1); foreach $i (1 .. $count) { $length = unpack("C", $bytes); $str = unpack("xA$length", $bytes); # null byte skips length $bytes = substr($bytes, $length + 1); print "$str\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Faster way to parse binary stream
by BrowserUk (Patriarch) on Jun 19, 2007 at 19:50 UTC | |
by ikegami (Patriarch) on Jun 19, 2007 at 20:29 UTC | |
by BrowserUk (Patriarch) on Jun 19, 2007 at 21:29 UTC | |
by Roy Johnson (Monsignor) on Jun 19, 2007 at 20:29 UTC | |
by Anonymous Monk on Jun 19, 2007 at 21:27 UTC | |
|
Re: Faster way to parse binary stream
by ikegami (Patriarch) on Jun 19, 2007 at 19:50 UTC |