Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin parse Hex String to Long

Tags:

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?

like image 654
Mark Gilchrist Avatar asked Jan 14 '17 15:01

Mark Gilchrist


People also ask

What is the correct syntax to convert the string 42 to a long in Kotlin?

toLongOrNull() to convert the string to a Long , return a null if the string is not a valid representation of a Long.

How do you convert hex to bytes?

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.


1 Answers

Since Kotlin v1.1 you can use:

"ED05265A".toLong(radix = 16) 

Until then use the Java's Long.parseLong.

like image 52
voddan Avatar answered Oct 07 '22 17:10

voddan