views:

31

answers:

1

I have a string coming from a database, for example 0b0101000.

I'd like to cast it to a binary value, in order to apply byte operations on it, like 0b01011000 & (1<<0 | 1<<4)

+6  A: 

Strings support a .to_i(base) method:

irb(main):016:0> b="0b01" ; b.to_i(2)
=> 1
irb(main):017:0> b="0b10" ; b.to_i(2)
=> 2
irb(main):018:0> b="0b11" ; b.to_i(2)
=> 3
irb(main):019:0> b="0b1111" ; b.to_i(2)
=> 15
sarnold
How could I ever miss that ? Thanks.
jlecour