Byte declaration in Kotlin makes compile-time error "Integer literal does not match expected Byte type"

As you can see, another question at 0xFF0000FF Integer literal does not match the expected kotlin.Int type

I declare the value 0xFF as Byte

val b:Byte = 0xFF

Getting Integer literal does not match expected Byte type Compilation error

Since I have the key to the kotlin.Byte

signed byte

How to declare byte in Kotlin with 0x00

- 0xFF

unsigned byte like Byte b = 0xFF

?

...

...

If you need a compiler to test and answer:

rextester , try.kotlinlang.org

+3


source to share


1 answer


Kotlin does not automatically convert between number types.

If you do val b:Byte = 0xFF.toByte()

, then it compiles and outputs a byte with a value -1

.



If you want to store the unsigned byte value, you need to store it as Char

, but you will need to convert it to another type in order to print it as a number:

val b = 0xFF.toChar()
println(b.toInt())

      

+4


source







All Articles