in reply to Faster way to parse binary stream
If the entire string (after the first 'no of strings' byte) is made up length/string pairs, then (subject to using an unenbalmed version of Perl) let unpack take care of it. Ignore/skip the first byte and use the ()repeat pattern to deal with it. Eg:
$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);; print for unpack 'x(c/a*)*', $bytes;; First string Second string Third string
If there is other stuff following the strings, then you'll need to unpack the first byte and then generate the pattern:
$n = unpack 'C', $bytes;; print for unpack 'x (c/a*)' . $n, $bytes;; First string Second string Third string
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Faster way to parse binary stream
by ikegami (Patriarch) on Jun 19, 2007 at 20:29 UTC | |
by BrowserUk (Patriarch) on Jun 19, 2007 at 21:29 UTC | |
|
Re^2: Faster way to parse binary stream
by Roy Johnson (Monsignor) on Jun 19, 2007 at 20:29 UTC | |
by Anonymous Monk on Jun 19, 2007 at 21:27 UTC |