From: Daniel Martin Date: 2006-07-25T00:56:44+09:00 Subject: Re: How can I parse binary files? Fabio Vitale writes: > This time I'm trying to write a binary file. > > Q1: why does the structure MRKMessageFlags does not get the apropriate > values? I'm not sure. It appears to be a bug in bit-struct. Here's a workaround; in your code replace the bit that sets the flag bits with: # work around bit-struct nested types bug flags = msg.flags flags.flagSeen = 1 flags.flagFlagged = 1 msg.flags = flags > Q2: how do I convert a date to seconds-since-1970? Don't assign date a string, assign it a Time object. The easiest way to get one of those is with Time.parse: msg.date = Time.parse("Mon Jul 24 12:34:04 2006") Your file loop now looks like this: (I also added calls to write out the data to the file) File.open("imap2.mrk", "wb") {|f| # mrk_header = MRKHeader.new() mrk_header.version = 1 mrk_header.uid_Validity = 1106138982 mrk_header.uid_next = 5887 mrk_header.last_write_counter = 9962 # I omitted modifying unused since in theory we don't need to # change the last two bytes to "\r\n" - it is unused, after all puts mrk_header.inspect f.write(mrk_header) msg = MRKMessage.new() msg.filename = "md50000006021.msg" # work around bit-struct nested types bug flags = msg.flags flags.flagSeen = 1 flags.flagFlagged = 1 msg.flags = flags msg.uid = 5885 msg.msg_size = 4184 msg.date = Time.parse("Mon Jul 24 12:34:04 2006") puts msg.inspect f.write(msg) }