From: ahoward Date: 2003-05-06T06:42:01+09:00 Subject: Re: [Q] Binary data On Tue, 6 May 2003, Daniel Carrera wrote: > Hi, > > Is it possible to store and manipulate binary data in Ruby? > As in the actual sequence of 1's and 0's. I'm curious in the > data-compression problem, but to be able to do any compression on a Ruby > string I need to be able to manipulate data at the bit level. > > Does anyone know how this can be done? ----CUT---- #!/usr/bin/env ruby # http://rwiki.moonwolf.com/rw-cgi.cgi?cmd=view;name=BitSet # http://raa.ruby-lang.org/list.rhtml?name=bitset require 'bitset' class BitSet; def []= i, b; self.set i, b; end; end b = BitSet.new 8 b[1], b[3], b[5] = 1, 1, 1 p b # >> 01010100 p eval("#{b}b0".reverse) # >> 42 ----CUT---- -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================