Binary to Decimal Conversion (binToDec)
binToDec is written in Ruby, and converts a binary number to a decimal only if the argument is binary. If the argument is not binary, an exception is raised. It should be considered an experimental gem.
Installation
Add this line to your application's Gemfile:
gem 'binToDec'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install binToDec
Usage
Converter.binToDec(1000)
Converter.bintoDec(2)
Basis
The program uses the following test to determine if the input is binary:
arg1.to_s.split(//).map { |i| i.to_i }.find_all { |value| value > 0 }.inject(:*)
If the test passes, the program uses the following code to convert the input (arg1) to binary.
arg1.to_s.split(//).map { |i| i.to_i }.inject(0) { |accumulator, value| (accumulator + value) * 2 }
return result/2
Contributing
Written by Thomas Dowling (tomgdow)
The National College of Ireland