mCY mCY - 2 months ago 35
Scala Question

Long/Int in scala to hex string

With

Integer.toString(1234567, 16).toUpperCase() // output: 12D68
could help to convert an
Int
to Hex string.

How to do the same with Long?

Long.toString(13690566117625, 16).toUpperCase() // but this will report error

Answer

You're looking for RichLong.toHexString:

scala> 13690566117625L.toHexString
res0: String = c73955488f9

And the uppercase variant:

scala> 13690566117625L.toHexString.toUpperCase
res1: String = C73955488F9

Edit

This also available for Int via RichInt.toHexString:

scala> 42.toHexString
res4: String = 2a
Comments