How to convert a string or integer to binary in Ruby?

后端 未结 8 1387
栀梦
栀梦 2020-11-30 17:22

How do you create integers 0..9 and math operators + - * / in to binary strings. For example:

 0 = 0000,
 1 = 0001, 
 ...
 9 = 1001

Is the

8条回答
  •  [愿得一人]
    2020-11-30 18:00

    If you are looking for a Ruby class/method I used this, and I have also included the tests:

    class Binary
      def self.binary_to_decimal(binary)
        binary_array = binary.to_s.chars.map(&:to_i)
        total = 0
    
        binary_array.each_with_index do |n, i|
          total += 2 ** (binary_array.length-i-1) * n
        end
        total
       end
    end
    
    class BinaryTest < Test::Unit::TestCase
      def test_1
       test1 = Binary.binary_to_decimal(0001)
       assert_equal 1, test1
      end
    
     def test_8
        test8 = Binary.binary_to_decimal(1000)
        assert_equal 8, test8
     end
    
     def test_15
        test15 = Binary.binary_to_decimal(1111)
        assert_equal 15, test15
     end
    
     def test_12341
        test12341 = Binary.binary_to_decimal(11000000110101)
        assert_equal 12341, test12341
     end
    end
    

提交回复
热议问题