https://kotlinlang.org logo
#announcements
Title
# announcements
s

Sylvain Patenaude

12/03/2019, 8:55 PM
Is there a way in Kotlin to explicitly declare a
Byte
or
UByte
literal? I know that
val toto: UByte = 128u
works when it's an assignment, but I'm looking to make something like this compile:
Copy code
while (myUByteArray[index] and 128u == 128u) {
}
without having to do an explicit cast like
128u.toUByte()
. Thanks! EDIT: According to https://kotlinlang.org/docs/tutorials/kotlin-for-py/primitive-data-types-and-their-limitations.html, I can't without an explicit conversion. I think I will just put the
128u
into a local
val
.
d

Dico

12/04/2019, 7:37 AM
I would go with
myUByteArray[index].toUInt()
And compare with
0x80u
Are you trying to read a varint? :)
s

Sylvain Patenaude

12/04/2019, 2:10 PM
Yeah, something similar to a varint from a protocol buffer encoding. But I settled with a private UByte constant of 128u (or 0x80u).
7 Views