Hex String to Int,Short and Long in Scala

scala

Solution

You can use the Java libs:

val number = Integer.parseInt("FFFF", 16)
> number: Int = 65535

Or if you are feeling sparky :-):

implicit def hex2int (hex: String): Int = Integer.parseInt(hex, 16)

val number: Int = "CAFE" // <- behold the magic
number: Int = 51966

Also, if you aren't specifically trying to parse a String parameter into hex, note that Scala directly supports hexadecimal Integer literals. In this case:

val x = 0xCAFE
> x: Int = 51966

Isn't Scala wonderful? :-)

Problem

Just can't find a way to transform an Hex String to a number (Int, Long, Short) in Scala. Is there something like `"A".toInt(base)`?

Original source