From: Charles Mills Date: 2005-01-09T09:51:25+09:00 Subject: Re: file io question Mark Firestone wrote: > I'm writing a fido mail tosser for my ruby bbs. (A thankless task that > is far worse than I ever imagined when I started.) and I have a problem > I don't understand. > > To read the packets (the header in this case, I have to do the > following, is it is stored in an odd way. Here's a snippet of the file > header... > > PP� > (and lots of other low ascii characters...) > > So, I read it like this, suck in a bunch of it in a buffer... > > buffer = happy.read(0x3a) > > orgnode = (buffer[0x01] << 8) + buffer[0x00] > destnode = (buffer[0x03] << 8) + buffer[0x02] > orgnet = (buffer[0x15] << 8) + buffer[0x14] > destnet = (buffer[0x17] << 8) + buffer[0x16] > orgzone = (buffer[0x23] << 8) + buffer[0x22] > destzone = (buffer[0x25] << 8) + buffer[0x24] > > and so on. This works great, we get the right answer... > > if I try to reverse this and write stuff, it doesn't work. For > example... > > happy = File.new("#{path}", File::CREAT|File::APPEND|File::RDWR, 0644) > > buffer = [] should this be: buffer = '' > > buffer[0x00] = (f_hdr.orgnode & 0x00ff) > buffer[0x01] = ((f_hdr.orgnode & 0xff00) >> 8) > buffer[0x02] = (f_hdr.destnode & 0x00ff).chr > buffer[0x03] = ((f_hdr.destnode & 0xff00) >> 8) > buffer[0x04] = ((f_hdr.year + 1900) & 0x00ff) > buffer[0x05] = (((f_hdr.year + 1900) & 0xff00) >> 8) > buffer[0x06] = f_hdr.month > buffer[0x08] = f_hdr.day > buffer[0x0a] = f_hdr.hour > > and so on and then... > > happy.write buffer (...) Sounds like you should be using String#unpack and Array#pack. -Charlie