andrew_queisser / hp.com (andrew queisser) writes:

> How can I convert a string of binary digits to a number?
> I've got this but I imagine there's some kind of 
> built-in method. Is there?
> 
> def BinStrToNum ( binStr )
>   sum = 0
>   digit = 1
>   binStr.reverse.each_byte { |i|
>     sum += (i-48)*digit
>     digit <<= 1
>   }
>   return sum
> end

If you have a leading '0b', you can use the Integer method

  Integer("0b010110111")		# => 183

With Ruby 1.7, you can also use String#to_i, passing in the base

  "10101".to_i(2)


Cheers


Dave