Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何将十六进制数作为输入?我的意思不是将整数作为输入并将其转换为十六进制,而是将十六进制数作为输入。
从标准输入读取十六进制数并解析:gets.to_i(16). 我认为您真正要问的是如何做到这一点:
gets.to_i(16)
x = gets.to_i(16) x = x + 1 puts x.to_s(16)
Ruby 默认所有解析/打印为十进制;如果你想要十六进制,你必须明确。
这?
>> "0xfa".hex => 250
或这个?
>> ((0x4009)+1).to_s(16) => "400a"