Hexadecimal to Decimal Terminal Script


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def hex_to_decimal(x)
  2. x = x.upcase.scan(/[0-9A-F]/).reverse
  3. y = 0
  4. z = 0
  5. i = 0
  6. x.each do |n|
  7. x[i] = case n
  8. when 'A'
  9. then x[i] = 10
  10. when 'B'
  11. then x[i] = 11
  12. when 'C'
  13. then x[i] = 12
  14. when 'D'
  15. then x[i] = 13
  16. when 'E'
  17. then x[i] = 14
  18. when 'F'
  19. then x[i] = 15
  20. else
  21. n = n.to_i
  22. end
  23. z = z + (n * (16**y))
  24. y += 1
  25. i += 1
  26. end
  27. z
  28. end
  29.  
  30. number = gets.chomp
  31. puts hex_to_decimal(number)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.