Kotlin parse Hex String to Long

2019-04-25 02:30发布

问题:

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:

This is coming in Kotlin v1.1:

"ED05265A".toLong(radix = 16)

Until then use the Java's Long.parseLong.