From: Joel VanderWerf Date: 2009-07-08T08:34:50+09:00 Subject: Re: Question on networking with custom binary interface. Greg Chambers wrote: > Thanks for help of the first question. As for the second question, no > the field is not fixed length. The only guarantees are that the > string is the only variable length field, it is null terminated, it's > start byte is known, and the length of the entire packet is known. > Any ideas? Was afraid of that... it's kind of a tricky case. The only way to read a field after the variable length field is to scan to the null terminator and then jump from that to a known offset, right? And it gets worse with each such field. So the accessor-based approach of bit-struct becomes complicated and inefficient. You might be better of pre-parsing the data into blocks of fixed and variable length fields. Then use BitStruct classes to parse the former. Wrap your own class (even just an array with positional accessors) around this and it could be fairly nice. Ara Howard had a suggestion along those lines, using pack/unpack instead of bit-struct, in: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152096 That example wasn't for variable length fields, but it would apply. The key point is that instead of leaving the data as a string and accessing fields as substrings (like bit-struct does), you parse it into an array, access the entries in the array, and write it back to a string when needed. Maybe a hybrid would work for you... parse into an array of BitStructs and variable length Strings. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407