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?
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
I can think of the following:
Bit operations w/ Fixnum, then Array#pack.
···
On Tue, May 06, 2003 at 05:17:01AM +0900, 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?
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
- LG loves czech girls.
LG: do they have additional interesting “features” other girls don’t have?
– #Debian
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?
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
···
From: “Daniel Carrera” dcarrera@math.umd.edu
----CUT----
#!/usr/bin/env ruby
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
···
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?
–
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
====================================
Hi Daniel
Daniel Carrera wrote:
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.
You should have a look at the following methods:
Array#pack
String#unpack
Hope this helps,
···
–
Laurent
Is it possible to store and manipulate binary data in Ruby?
[snip]
I can think of the following:
Bit operations w/ Fixnum, then Array#pack.
Could you explain to me how to use Array#pack? PickAxe doesn’t contain
almost anything:
Pickaxe:
a = [ “a”, “b”, “c” ]
n = [ 65, 66, 67 ]
a.pack(“A3A3A3”)" # → “a b c”
a.pack(“a3a3a3”) # → “a\000\000b\000\000c\000\000”
n.pack(“ccc”) # → “ABC”
Where can I learn how to use Array#pack?
Thanks,
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
Bill Kelly wrote:
Hi,
From: “Daniel Carrera” dcarrera@math.umd.edu
Just a quick example:
x = “hello”.unpack(“b*”).to_s →
“0001011010100110001101100011011011110110”
x[0] → 49 (i.e. 00010110)
Also, see sig. Hope that helps.
Regards,
Dan
···
–
p
[“010100101010111011001110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110”].pack(“b*”)
It’s in the Pickaxe, you just have to look a bit…
http://www.rubycentral.com/book/ref_c_array.html
search for “Template characters for Array#pack” which is a table of the
different packing codes. In the HTML it sits just after the definition of
‘include?’, which is inconveniently not next to ‘pack’…
The counterpart is String#unpack:
http://www.rubycentral.com/book/ref_c_string.html#String.unpack
(and its table of template characters is more sensibly placed
But if you just want to pick individual bits out of a binary String you
don’t need either:
a = “hello”
a[0] # 104 (= 0x68 = ASCII code for ‘h’)
a[1] # 101 (= 0x65 = ASCII code for ‘e’)
a[1][0] # 1 (bit 0 of 0x65)
a[1][1] # 0 (bit 1 of 0x65)
a[1][2] # 1 (bit 2 of 0x65)
etc
i.e. String# extracts one byte as a Fixnum, and Fixnum# extracts one
bit as 0 or 1
Regards,
Brian.
···
On Tue, May 06, 2003 at 06:10:40AM +0900, Daniel Carrera wrote:
Could you explain to me how to use Array#pack? PickAxe doesn’t contain
almost anything:
Pickaxe:
a = [ “a”, “b”, “c” ]
n = [ 65, 66, 67 ]
a.pack(“A3A3A3”)" # → “a b c”
a.pack(“a3a3a3”) # → “a\000\000b\000\000c\000\000”
n.pack(“ccc”) # → “ABC”
Where can I learn how to use Array#pack?