https://kotlinlang.org logo
Title
m

Marcin Wisniowski

02/14/2019, 3:19 PM
java.lang.Long.parseLong()
Why isn't this available in the Kotlin
Long
?
t

thana

02/14/2019, 3:21 PM
Because "5".toLong() does the job
1
m

Marcin Wisniowski

02/14/2019, 3:21 PM
Thank you 😄
t

thana

02/14/2019, 3:21 PM
🙂
m

Marcin Wisniowski

02/14/2019, 3:24 PM
But what about
parseUnsignedLong()
? (I am parsing a binary data stream so I need to differentiate). There is a
.toULong()
but it actually returns an
ULong
which I don't want to use because it's marked as experimental. The Java
parseUnsignedLong()
returns a normal
Long
. I guess I either have to use
ULong
or use the Java method.
d

Davio

02/14/2019, 3:28 PM
toULong().toLong()
m

Marcin Wisniowski

02/14/2019, 3:30 PM
Still makes Idea shout at me for using experimental types but makes my function return the correct type so it will do. Thank you.
j

Jurriaan Mous

02/14/2019, 3:32 PM
Mind that a large ULong does not fit inside a Long. (Since it goes further into positive number ranges which it does not need to go into the negative) So if you do that it is maybe just enough to do toLong() and check if number is positive.
d

Davio

02/14/2019, 3:34 PM
For numbers larger than Long.MAX_VALUE, they get wrapped around, so ULong.MAX_VALUE == -1 (Long)
m

Marcin Wisniowski

02/14/2019, 3:37 PM
Yes I parse a string of binary (with radix = 2), checking the number of bits beforehand against
Long.SIZE_BYTES
. Seems to work so far.