user1616238
user1616238

Reputation:

Take hexadecimal number as input?

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

Answers (2)

tokland
tokland

Reputation: 67850

This?

>> "0xfa".hex
=> 250

Or this?

>> ((0x4009)+1).to_s(16)
=> "400a"

Upvotes: 2

Andy
Andy

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

Related Questions