在 Scala 中将 Long 转换为 base 36
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14949732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a Long to base 36 in scala
提问by AKnox
How can I convert a Long to base36 ? Along with your answer can explain how you came to the answer?
如何将 Long 转换为 base36 ?连同你的答案可以解释你是如何得出答案的吗?
I've checked the scaladocs for Longfor converting to a different base, and on converting a Long to a BigInt. I saw that BigIntdoes have toString( base )so a solution could involve changing the type, but I couldn't figure out how.
我已经检查了Long的scaladocs 以转换为不同的基础,以及将 Long 转换为 BigInt。我看到BigInt确实有toString( base )所以解决方案可能涉及更改类型,但我无法弄清楚如何。
Note: I'm new to scala / java / type-safe languages so I could be overlooking something trivial.
注意:我是 scala / java / 类型安全语言的新手,所以我可能会忽略一些琐碎的事情。
回答by charo
Staying within Scala language, converting Longto a BigIntturns out to be simple:
留在 Scala 语言中,转换Long为 aBigInt原来很简单:
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 BigInthad all the stuff. After reviewing scaladoc and the answers above, a few fails followed.
我进入主题的方式是作为 Java 开发人员和 Scala 初学者(阅读“Scala for the Impatient.pdf”的第 1 章)。上面的答案有效,但似乎很奇怪。不得不跳入 Java 领域进行这样的基本转换似乎不太正确,尤其是在BigInt拥有所有东西的情况下。在查看了 scaladoc 和上面的答案后,出现了一些失败。
My biggest fail was using toInt()which truncates myLonghorridly. About to give up, the final attempt seemed so simple and intuitive that I almost didn't try it: myBigInt = myLong. Perhaps one day Longwill be richer and understand toBigInt... this was my first fail in the journey.
我最大的失败是使用toInt()which 截断myLong可怕。快要放弃了,最后的尝试看似简单又直观,差点没去尝试:myBigInt = myLong。也许有一天Long会更丰富和理解toBigInt......这是我在旅途中的第一次失败。
回答by Josh Marcus
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.
类 java.lang.Long 有一个静态方法 toString(long i, int radix),它将把 Long 转换为另一个基的字符串表示。在这种情况下,“基数”与“基数”的含义相同。
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
Scala 会在必要时将您的 scala Long 值视为 java.lang.Long ,因此您可以随时在必要时在 Java API 文档中查找方法。请参阅:http: //docs.oracle.com/javase/6/docs/api/java/lang/Long.html
回答by climbing_bum
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.
所以你想把 Long 转换成 BigInt。BigInt 对象有一个将 Long 作为参数的 apply 方法。这是 REPL 中的一个示例。
scala> BigInt(458982948578L).toString(36)
res11: String = 5uuqlhpe
回答by Daniel C. Sobral
Well, either the method is available on Scala's Longor its enhancement class, RichLong, or you must search for it on the Java counterpart. The latter happens to be the case.
好吧,要么该方法在 ScalaLong或其增强类中可用RichLong,要么您必须在 Java 对应物上搜索它。后者恰好是这种情况。
It could be either on the source type or the destination type, and since longis 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 Stringon it.
它可以在源类型或目标类型上,并且由于long它不是 Java 中的类,因此您必须在java.lang.Long. 它不在String-- 寻找采用的方法Long-- ,但您可以在 上找到它java.lang.Long,只需寻找返回的方法即可String。
回答by samthebest
General form
一般形式
def nextInt(d: Double): Int = if (math.floor(d) == d) d.toInt + 1 else math.ceil(d).toInt
def digitsL(base: Int, n: Long, padTo: Int = 0): List[Int] =
List.fill(List(padTo, nextInt(math.log(n) / math.log(base))).max - 1)()
.foldLeft((List((n % base).toInt), base)) {
case ((cum, div), _) => (((n / div) % base).toInt +: cum, div * base)
}._1
(And the reverse)
(反之亦然)
def digitsToLong(base: Int, digits: List[Int]): Long = digits.foldRight((0L, 1)){
case (cur, (cum, pow)) => (cum + cur.toLong * pow, pow * base)
}._1

![scala 比较 Option[T] 的实例,避免 None == None](/res/img/loading.gif)