0

In Scala, I can't seem to convert hex-strings back to integers:

val cols = Array(0x2791C3FF, 0x5DA1CAFF, 0x83B2D1FF, 0xA8C5D8FF,
      0xCCDBE0FF, 0xE9D3C1FF, 0xDCAD92FF, 0xD08B6CFF,
      0xC66E4BFF, 0xBD4E2EFF)

cols.map({ v => Integer.toHexString(v)}).map(v => Integer.parseInt(v, 16))

I get the following error message:

java.lang.NumberFormatException: For input string: "83b2d1ff"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:583)
  at $anonfun$3.apply(<console>:12)
  at $anonfun$3.apply(<console>:12)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
  at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
  at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
  at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
  at scala.collection.mutable.ArrayOps$ofRef.map(ArrayOps.scala:186)
  ... 35 elided

2 Answers 2

1

Int is too small. Use Long.

scala> 0x83B2D1FFDL
res3: Long = 35352551421

or

scala> java.lang.Long.decode("0x83B2D1FFD")
res4: Long = 35352551421

and back

scala> java.lang.Long.toHexString(res3)
res5: String = 83b2d1ffd
Sign up to request clarification or add additional context in comments.

Comments

1

Try it as unsigned values.

cols.map(Integer.toHexString).map(Integer.parseUnsignedInt(_, 16))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.