1

I was surprised that the following forward backward conversion from 32-bit ints to hex strings fails:

Integer.parseInt(-2028332484.toHexString, 16)

Gives:

java.lang.NumberFormatException: For input string: "871a1a3c"

Obviously a workaround is

java.lang.Long.parseLong(-2028332484.toHexString, 16).toInt

But I wonder if there is not a better (and possibly more Scala'ish) solution?

0__
  • 66,707
  • 21
  • 171
  • 266

1 Answers1

0

It's been answered for Java already here.

Unfortunately there is no additional treatment for that transformation in scala AFAIK.

Scala defines in RichInt:

def toHexString: String = java.lang.Integer.toHexString(self)

and in StringLike:

def toInt: Int         = java.lang.Integer.parseInt(toString)

apart from import java.lang.{Long => JLong} and using JLong I don't know a more scala-ish solution than yours.

Community
  • 1
  • 1
michael_s
  • 2,515
  • 18
  • 24
  • Thanks for the link. I see that the symmetric counterpart of `parseInt(_, 16)` is indeed `toString(_, 16)`, and there is no counterpart for `toHexString`. – 0__ Apr 07 '13 at 11:58