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?
toLongOrNull() to convert the string to a Long , return a null if the string is not a valid representation of a Long.
To convert hex string to byte array, you need to first get the length of the given string and include it while creating a new byte array. byte[] val = new byte[str. length() / 2]; Now, take a for loop until the length of the byte array.
Since Kotlin v1.1 you can use:
"ED05265A".toLong(radix = 16)
Until then use the Java's Long.parseLong
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With