Posted By

AzizLight on 09/04/09


Tagged

textmate decimal ruby terminal binary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

imm


Binary to decimal terminal script


 / Published in: Ruby
 

  1. def binary_to_decimal(x)
  2. y = 1
  3. z = 0
  4. x.gsub(/ /,'').reverse.scan(/(0|1)/).each do |x|
  5. z = z + (y * x[0].to_i)
  6. y *= 2
  7. end
  8. z
  9. end
  10.  
  11. number = gets.chomp
  12. puts binary_to_decimal(number)

Report this snippet  

You need to login to post a comment.