From: Joel VanderWerf Date: 2006-07-30T00:43:09+09:00 Subject: Re: How can I parse binary files? Hi, all. Sorry to respond so late to this thread. Thanks to Daniel for his excellent and thorough responses! Just one comment below on Daniel's solution... Daniel Martin wrote: > require 'bit-struct' > class MRKHeader < BitStruct > unsigned :version, 32, "Version", :endian => :native > unsigned :uid_Validity, 32, "UIDValidity", :endian => :native > unsigned :uid_next, 32, "UIDNext", :endian => :native > unsigned :last_write_counter, 32, "LastWriteCounter", :endian => :native > rest :unused, "Unused" > # Override so that it gets padded properly > def MRKHeader.round_byte_length > super > 36 > end > end I'd suggest defining a fixed-length "unused" field instead of a "rest" field. The rest construct is better for variable length data, such as a payload at the end of a packet. So instead of rest :unused, "Unused" you can consume the bytes with char :unused, (36*8-4*32), "Unused" Then you don't have to worry about whether overriding #round_byte_length is the right thing to do or not. The disadvantage to doing it this way is that inspect will print out stuff you don't care about. Solving this problem gets to the suggestions Daniel made in another post, so I'll respond separately. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407