I have a packaged string of three lines, which is structured so that I have an integer specifying the byte length of the next element, and then the bytes of the element, and then the next byte element, etc., as if someone did something
[a.bytesize, a, b.bytesize, b, c.bytesize, c].pack("na*na*na*")
How can I unpack this correctly in a simple way? Perl solution for this problem:
my($a, $b, $c) = unpack("(n/a*)3", $data)
For ruby, which apparently does not support "/" and parentheses in unpacking, I use something like:
vals = []
3.times do
size = data.unpack("n").first
data.slice!(0, 2)
vals << data.unpack("a#{size}").first
data.slice!(0, size)
end
Is there an easier way to do this?
Speed source
share