/ Published in: Ruby

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def hex_to_decimal(x) x = x.upcase.scan(/[0-9A-F]/).reverse y = 0 z = 0 i = 0 x.each do |n| x[i] = case n when 'A' then x[i] = 10 when 'B' then x[i] = 11 when 'C' then x[i] = 12 when 'D' then x[i] = 13 when 'E' then x[i] = 14 when 'F' then x[i] = 15 else n = n.to_i end z = z + (n * (16**y)) y += 1 i += 1 end z end number = gets.chomp puts hex_to_decimal(number)
Comments
