Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Long to base 36 in scala

How can I convert a Long to base36 ? Along with your answer can explain how you came to the answer?

I've checked the scaladocs for Long for converting to a different base, and on converting a Long to a BigInt. I saw that BigInt does have toString( base ) so a solution could involve changing the type, but I couldn't figure out how.

Note: I'm new to scala / java / type-safe languages so I could be overlooking something trivial.

like image 687
AKnox Avatar asked Feb 19 '13 04:02

AKnox


4 Answers

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.

like image 95
charo Avatar answered Nov 02 '22 02:11

charo


The class java.lang.Long has a static method toString(long i, int radix) which will convert a Long into a string representation of another base. "Radix" means the same thing as "base" in this context.

val myLong = 25000L
val longInBase36:String = java.lang.Long.toString(myLong, 36)

Scala will treat your scala Long value as a java.lang.Long when necessary, so you can always look for methods in the Java API documentation when necessary. See: http://docs.oracle.com/javase/6/docs/api/java/lang/Long.html

like image 28
Josh Marcus Avatar answered Nov 02 '22 04:11

Josh Marcus


So you want to convert Long to BigInt. BigInt object has an apply method that takes Long as a parameter. Here is an example in the REPL.

scala> BigInt(458982948578L).toString(36)
res11: String = 5uuqlhpe
like image 8
climbing_bum Avatar answered Nov 02 '22 02:11

climbing_bum


Well, either the method is available on Scala's Long or its enhancement class, RichLong, or you must search for it on the Java counterpart. The latter happens to be the case.

It could be either on the source type or the destination type, and since long is not a class in Java, you'd have to look for it on java.lang.Long. It's not on String -- look for methods taking Long --, but you can find it on java.lang.Long, just looking for methods returning String on it.

like image 5
Daniel C. Sobral Avatar answered Nov 02 '22 03:11

Daniel C. Sobral