Issue
I am working on a small project where i required to decode the data coming as a Hexadecimal format in Scala Spark. For that I need to convert two Hexadecimal character's to signed number i.e "F0" to "-16" using Scala.(1 byte string) And need to convert 2 byte i.e. four char hexadecimal string to signed number. But when I am trying to convert It was showing "Value out of range" Exception. Could you suggest me a simple way to do it. Thank you.
Solution
You just need to parse it to a larger container, and then downcast:
val byteVal = java.lang.Long.valueOf("f0", 16).toByte // -16
val shortVal = java.lang.Long.valueOf("fff0", 16).toShort // -16
val intVal = java.lang.Long.valueOf("fffffff0", 16).toInt // -16
Answered By - Dima
Answer Checked By - Terry (JavaFixing Volunteer)