 # Binary to Decimal Conversion in Ruby

Published: May 04, 2017 • Updated: December 30, 2022 2 min read

• ruby
• algorithms

This week I wrote an algorithm in Ruby to convert binary numbers into decimal numbers. Here’s the problem description, from Exercism:

“Convert a binary number, represented as a string (e.g. ‘101010’), to its decimal equivalent using first principles. Implement binary to decimal conversion. Given a binary input string, your program should produce a decimal output. The program should handle invalid inputs.”

### Real World

If this was a real-world Ruby problem, I’d convert from binary to decimal with `.to_i`:

``````> '101010'.to_i(2)
=> 42``````

And back to binary with `.to_s`:

``````> 42.to_s(2)
=> '101010'``````

### My Algorithm

But this is an algorithm challenge, so here’s my algorithm.

``````class Binary
def self.to_decimal(binary)
raise ArgumentError if binary.match?(/[^01]/)

binary.reverse.chars.map.with_index do |digit, index|
digit.to_i * 2**index
end.sum
end
end``````

And the usage:

``````> Binary.to_decimal('101010')
=> 42``````

What’s going on here? Examining each line:

1. We make a class `Binary`, a container for our code.
2. We define a class method. I prefer a function when object instantiation isn’t beneficial.
3. Guard clause! Raise an `ArgumentError` if the string does not match the binary format.
4. Space; let that code breathe.
5. Reverse the string and split into characters, then `.map` over each with index included.
6. Take each digit, starting with the rightmost because the power grows with each position. Convert it into a number, and multiply that number by two to the power of its enumerable index. In simpler terms, convert each digit to its individual decimal value.
7. Sum the result.

### What’s Good

I like that my solution matches for `ArgumentError` positively, covering a range of bad inputs not specified by the Exercism test cases. I like that it reverses the string so that index can be used as the incrementing exponent. I like that it uses `.map` to return a result without an accumulator variable.

### What I’d Do Differently Next Time

I may have golfed this down a little too much. In general, I think mathematical functions can get away with terser syntax than ordinary functions, because the principles are well-defined in another domain.

### Wrapping Up

I hope this post helped you learn how convert binary to decimal and back in Ruby a few ways. Do you do it a different way? Let me know on Twitter.

Get better at programming by learning with me. Subscribe to my newsletter for weekly ideas, creations, and curated resources from across the world of programming. Join me today!