From: "yermej@..." Date: 2007-10-26T01:17:11+09:00 Subject: Re: read write integer in binary into a file On Oct 25, 9:36 am, 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. > -- > Posted viahttp://www.ruby-forum.com/. Do you have to deal with each number individually? Maybe you could build up an array of numbers and then pack them all at once: arr = [] while work_to_do do mynum = generate_next_number arr << mynum end myfile.write arr.pack('i*') That way you aren't creating a new array for each number. Similarly, for reading the file: data = file.read num_array = data.unpack('i*') The '*' in (un)pack means to process the rest of the data in the same way.