Decimal to Hexadecimal Terminal Script


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



Copy this code and paste it in your HTML
  1. def decimal_to_hex(x)
  2. x = x.to_i
  3. q = 1
  4. r = []
  5. while q > 0
  6. q = x / 16
  7. r << x % 16
  8. x = q
  9. end
  10. i = 0
  11. r.each do |n|
  12. r[i] = case n
  13. when 10
  14. then r[i] = 'A'
  15. when 11
  16. then r[i] = 'B'
  17. when 12
  18. then r[i] = 'C'
  19. when 13
  20. then r[i] = 'D'
  21. when 14
  22. then r[i] = 'E'
  23. when 15
  24. then r[i] = 'F'
  25. else
  26. n = n
  27. end
  28. i += 1
  29. end
  30. r.reverse.join
  31. end
  32.  
  33. number = gets.chomp
  34. p decimal_to_hex(number)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.