/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def decimal_to_hex(x) x = x.to_i q = 1 r = [] while q > 0 q = x / 16 r << x % 16 x = q end i = 0 r.each do |n| r[i] = case n when 10 then r[i] = 'A' when 11 then r[i] = 'B' when 12 then r[i] = 'C' when 13 then r[i] = 'D' when 14 then r[i] = 'E' when 15 then r[i] = 'F' else n = n end i += 1 end r.reverse.join end number = gets.chomp p decimal_to_hex(number)