From 8bdb48f318464e79ea53563c90ee20b592b4f37d Mon Sep 17 00:00:00 2001 From: Kay N <30675614+kayxn23@users.noreply.github.com> Date: Tue, 21 Aug 2018 21:37:22 -0700 Subject: [PATCH] Create binary_to_decimal.rb --- binary_to_decimal.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 binary_to_decimal.rb diff --git a/binary_to_decimal.rb b/binary_to_decimal.rb new file mode 100644 index 0000000..154249b --- /dev/null +++ b/binary_to_decimal.rb @@ -0,0 +1,9 @@ +def binary_to_decimal(binary_array) #[01010100] 2**7 ~ 2**0 + sum = 0 + length = 8 + binary_array.each do |i| + length -= 1 + sum += i.to_i * (2**length) + end + sum +end