Reputation:
How can I take a hexadecimal number as input? I don't mean to take an integer as input and convert it to hexadecimal, but to take a hexadecimal number as input.
Upvotes: 0
Views: 1079
Reputation: 67850
This?
>> "0xfa".hex
=> 250
Or this?
>> ((0x4009)+1).to_s(16)
=> "400a"
Upvotes: 2
Reputation: 11985
To read a hex number from standard input and parse: gets.to_i(16)
. I think what you really meant to ask was how to do this:
x = gets.to_i(16)
x = x + 1
puts x.to_s(16)
Ruby defaults all parsing/printing to decimal; you have to be explicit if you want hex.
Upvotes: 4