Staying within Scala language, converting Long
to a BigInt
turns out to be simple:
var myLong : Long = Long.MaxValue
var myBigInt : BigInt = myLong
myBigInt.toString(36)
output being:
myLong: Long = 9223372036854775807
myBigInt: scala.math.BigInt = 9223372036854775807
res199: String = 1y2p0ij32e8e7
They way I came to topic was as a Java developer and Scala beginner (reading chapter 1 of "Scala for the Impatient.pdf"). The answers above worked but seemed strange. Having to jump into Java land for such a basic conversion seemed less correct somehow, especially when BigInt
had all the stuff. After reviewing scaladoc and the answers above, a few fails followed.
My biggest fail was using toInt()
which truncates myLong
horridly. About to give up, the final attempt seemed so simple and intuitive that I almost didn't try it: myBigInt = myLong
. Perhaps one day Long
will be richer and understand toBigInt
... this was my first fail in the journey.
def hasher(id: Long): String = { java.lang.Long.toString(id, 36) }
Thank you Josh! – Positronium