From: daz Date: 2004-03-02T13:49:45+09:00 Subject: Re: binary data "quillion" wrote: > Alwin Blok wrote: > > Hello! > > > > I'm new to ruby. I'm learning it and it seems to be a very nice language. > > > > However, I do have a question about binary handling. I'd like to write > > binary data directly to a file. > > I found that I can use myfile.write(["1001010100"].pack("B10")), but i'd > > like to be able to write, for example 0b1001010100 directly to a file. > > How do I do that, or else, are ther other useful ways to write binary > > data to a file > > > > Thanks in advance > > > > Alwin > > > very old coding style, may be of interest > > begin > out_file = your data > f = File.new("file name", "w") > end > > begin > out_file.each{ |ch| > f.binmode.putc(ch) > } > ensure > f.close > end > > > > neil Like Neil said ... File.open('tbinary.dat', 'wb') do |fb| fb.putc 0b11011011 fb.putc 255 fb.putc 0xAB fb.putc 0x42 end ## See what happened ... File.open('tbinary.dat', 'rb') do |fb| bd = fb.read puts bd.unpack('H*') end #-> dbffab42 daz