Search code examples
type-conversioncrystal-lang

Converting a hexadecimal string to a decimal number


In Ruby one can convert a hexadecimal string to a decimal number with String#hex method:

"1a2f".hex # => 6703

How to do that in Crystal?


Solution

  • Just use String#to_i with a base of 16:

    "1a2f".to_i(16) # => 6703