From: Wu Junchen Date: 2007-12-13T22:26:49+09:00 Subject: Re: read write integer in binary into a file Vianney Lecroart wrote: > Hello, > > I have some big files with lot of "unsigned int" (4 bytes) numbers and I > want to read and write on these files. > > Currently, I found this to write: > > myfile << [mynum].pack("i") > > and to read: > > mynum = myfile.read(4).unpack("i").first > > I wonder if there's not something faster/simpler to do that without the > need to convert the number into an array into a string to finally > serialize it. > > Thank you. irb(main):001:0> f=open('test','w') => # irb(main):002:0> f<<[65535].pack('i') => # irb(main):003:0> f.tell => 4 irb(main):004:0> f<<[720850].pack('i') => # irb(main):005:0> f.tell => 9 the integer 720850 takes 5 bytes in my file,but it should take 4 bytes only!How can I fix this?Thanks! -- Posted via http://www.ruby-forum.com/.