Forum: Ruby Binary String to Integer?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F62d0be4e8209c45a02d4f595014845a?d=identicon&s=25 Peter Marsh (killswitch)
on 2007-05-16 20:14
How can you translate a binary string into an integer? i.e.

two = '10'

string_to_int(two)

=> 2
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-05-16 20:19
Peter Marsh 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 topic is locked and can not be replied to.