Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Byte in Kotlin does compile-time error 'The integer literal does not conform to the expected type Byte'

Tags:

kotlin

As seen another question in 0xFF0000FF An integer literal does not conform to the expected type kotlin.Int

I declaring value 0xFF as Byte

val b:Byte = 0xFF

receiving The integer literal does not conform to the expected type Byte compile error

Since I have clue about kotlin.Byte is signed byte

How to declaring Byte in Kotlin with 0x00 - 0xFF unsigned byte just like .net Byte b = 0xFF ?

.

.

If you want instant compiler for testing and answer:

rextester , try.kotlinlang.org

like image 209
nyconing Avatar asked Jun 17 '17 12:06

nyconing


1 Answers

Kotlin doesn't automatically convert between number types.

If you do val b:Byte = 0xFF.toByte() then it compiles and results in a byte with value -1.

If you want to hold the unsigned byte value then you need to store it as a Char, but you will need to convert it to another type to print it as a number:

val b = 0xFF.toChar()
println(b.toInt())
like image 145
Kiskae Avatar answered Sep 18 '22 17:09

Kiskae