From: brabuhr@... Date: 2009-09-03T00:12:17+09:00 Subject: Re: confusing string parsing problem On Wed, Sep 2, 2009 at 10:01 AM, Matt Brooks wrote: > I don't understand how to do the following, any help is very > appreciated.  I have been programming in ruby 1.8.7 for about a month. > > I have a string that has in it: > 2 byte UINT16 > 2 byte UINT16 > 1 byte UINT8 representing how many pairs of the next 2 strings there are > variable length string1 <- Although for now I know this always equals > BFO > variable length string2 > 2 byte UINT16 > > I want to display each value from the fields, while also removing it > from the string as I read it out... > > My attempt is horrible I know, and I don't think it really runs. Can you post an example of your data and code with the incorrect output and expected output? > cat z.rb string = "" string << 0.chr << 1.chr string << 0.chr << 2.chr string << 2.chr string << "foo" << "BFO" string << "bar" << "BFO" string << 0.chr << 3.chr p string p string[0..1], string[0..1].unpack('n') p string[2..3], string[2..3].unpack('n') p string[0..3].unpack('n2') p string[4..4], string[4..4].unpack('C') p string[0..4].unpack('n2C') p string[5..-2].split(/BFO/) > ruby -v z.rb ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux] "\000\001\000\002\002fooBFObarBFO\000\003" "\000\001" [1] "\000\002" [2] [1, 2] "\002" [2] [1, 2, 2] ["foo", "bar", "\000"]