I have hex numbers, and I want to convert them into decimal numbers. For example, 01 -> 1, 09 -> 9, 12 -> 18.
I tried:
01.unpack("n")
but that failed.
"01".unpack("n") # => [12337]
That is not what I want.
Do you know the correct answer?
String#to_i
accepts an extra argument, which is the number base to use. Hexadecimal is base 16, so the following will work for you:
"01".to_i(16)
Calling the Integer
function on it will also work, so long as the number has an 0x
prefix:
Integer("0x01")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With