Return to Snippet

Revision: 17448
at September 4, 2009 19:40 by AzizLight


Updated Code
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)

Revision: 17447
at September 4, 2009 19:38 by AzizLight


Initial Code

                                

Initial URL

                                

Initial Description

                                

Initial Title
Hexadecimal to Decimal Terminal Script

Initial Tags
textmate, ruby, terminal

Initial Language
Ruby