问题
I am starting to work in Kotlin and I need to parse a hex String to a long, which in java can be done with
Long.parseLong("ED05265A", 16);
I can not find anything this in Kotlin, although I can find
val i = "2".toLong()
This is not what I am looking for!
before I write anything from scratch is there a built in function for this?
回答1:
You can simply use
java.lang.Long.parseLong("ED05265A", 16)
Or
import java.lang.Long.parseLong
[...]
parseLong("ED05265A", 16)
Kotlin is compatible with Java, and you can, and should, use Java's built-in classes and methods.
回答2:
Since Kotlin v1.1 you can use:
"ED05265A".toLong(radix = 16)
Until then use the Java's Long.parseLong.
来源:https://stackoverflow.com/questions/41651704/kotlin-parse-hex-string-to-long