Binary String to Integer?

How can you translate a binary string into an integer? i.e.

two = ‘10’

string_to_int(two)

=> 2

Peter M. wrote:

How can you translate a binary string into an integer? i.e.

two = ‘10’

string_to_int(two)

=> 2

irb(main):001:0> two = ‘10’
=> “10”
irb(main):002:0> two.to_i(2)
=> 2

The argument for #to_i is the base for converting the string to an int.

-Drew

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs