From: Rob Lee Date: 2006-10-23T22:45:56+09:00 Subject: Re: Binary file modification > It's already packed. The return value of the long expression will be a 3 > byte string which you can insert as you like (print it to the IO handle, > << > it to another string etc etc). The accumulator in the inject method is a > string {|str,byte| ... might have been more readable, sorry. > > ben Sorry I should have studied it a little closer ... I've tried the following and your re-packing doesn't seem to work : f = open("testfile","wb") num=359447 f.write(num.to_s(16)[0..2].instance_eval {(self.reverse + '0' * (6 - self.length)).reverse}.scan(/../).inject('') {|s,byte| s << byte.to_i(16)}) f.close f = open("testfile","rb") f.pos=0 puts f.read(3).unpack('H*').first.to_i(16) f.close => 1404 It does seem to work for small values like 1024 but not larger ones. I know there will be an upper limit to the integer size I can fit into the three bytes but the 359447 value is one I've taken from an existing file so I believe this is a valid integer to try and pack into these three bytes. Any advice ? -- Posted via http://www.ruby-forum.com/.