Hi, From: "Daniel Carrera" <dcarrera / math.umd.edu> > > 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? As I understand it, strings in Ruby store binary data, and can be viewed as an array of 8-bit bytes. >> dat = [0x12345678].pack("N") => "\0224Vx" >> printf("%x %x %x %x", dat[0], dat[1], dat[2], dat[3]) 12 34 56 78=> nil >> dat[1] ^= 0xFF # twiddle some bits in the 2nd byte => 203 >> printf("%x %x %x %x", dat[0], dat[1], dat[2], dat[3]) 12 cb 56 78=> nil >> dat.unpack("N") => [315315832] >> printf("%x", dat.unpack("N")[0]) 12cb5678=> nil So I don't think you necessarily have to use pack & unpack, unless you want to extract the binary data to some other format easily (or you need to work with non byte-width data I guess.) But if you start out with an empty string and write binary bytes to it, for instance, you should just be able to write the string to a file. If you read a binary file into a string, you should be able to tweak the bits on a byte-by-byte basis, and save the modified binary data in the string back out to a file... So I guess as long as byte-width access to binary data meets your needs, Ruby strings are pretty ideal. :) Hope this helps, Bill