From: Eric Wong Date: 2011-09-10T10:32:59+09:00 Subject: Re: how to write data in binary to a file? frank hi wrote: > I want to write the data into a file in binary mode. The file is opened > in binary mode by ofile=File.open("filename",'wb'). However, when I > write the data using ofile.write(1), ruby will write the ascii code 31 > to the file. I want it to write one byte number withe value 1. How can I > do it? I have been googling a lot and could not find the answer. You need to pack the data into a string before writing. The following will write one unsigned byte: buffer = [ 1 ].pack("C") file.write(buffer) See `ri Array#pack` for full documentation on things you can do with pack (and String#unpack for the inverse). There are also a nearly-identical `pack'/`unpack' functions in Perl. The "perlpacktut" manpage is a great read for this. -- Eric Wong