Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 541 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to correctly handle Byte values greater than 127 in Kotlin?

#1
Imagine I have a Kotlin program with a variable `b` of type `Byte`, into which an external system writes values greater than `127`. "External" means that I cannot change the type of the value it returns.

```val a:Int = 128
val b:Byte = a.toByte()```

Both `a.toByte()` and `b.toInt()` return `-128`.

Imagine I want to get the correct value (`128`) from the variable `b`. How can I do it?

In other words: What implementation of `magicallyExtractRightValue` would make the following test run?

@Test
fun testByteConversion() {
val a:Int = 128
val b:Byte = a.toByte()

System.out.println(a.toByte())
System.out.println(b.toInt())

val c:Int = magicallyExtractRightValue(b)

Assertions.assertThat©.isEqualTo(128)
}

private fun magicallyExtractRightValue(b: Byte): Int {
throw UnsupportedOperationException("not implemented")
}

**Update 1:** This solution suggested by [Thilo](

[To see links please register here]

) seems to work.

private fun magicallyExtractRightValue(o: Byte): Int = when {
(o.toInt() < 0) -> 255 + o.toInt() + 1
else -> o.toInt()
}
Reply

#2
Good old `printf` does what we want:

java.lang.String.format("%02x", byte)
Reply

#3
With Kotlin 1.3+ you can use [unsigned types][1]. e.g. [`toUByte`][2] ([Kotlin Playground][3]):

private fun magicallyExtractRightValue(b: Byte): Int {
return b.toUByte().toInt()
}

or even require using `UByte` directly instead of `Byte` ([Kotlin Playground][4]):

private fun magicallyExtractRightValue(b: UByte): Int {
return b.toInt()
}

For releases prior to Kotlin 1.3, I recommend creating an [extension function][5] to do this using [`and`][6]:

fun Byte.toPositiveInt() = toInt() and 0xFF

Example usage:

val a: List<Int> = listOf(0, 1, 63, 127, 128, 244, 255)
println("from ints: $a")
val b: List<Byte> = a.map(Int::toByte)
println("to bytes: $b")
val c: List<Int> = b.map(Byte::toPositiveInt)
println("to positive ints: $c")

Example output:

<!-- language: none -->

from ints: [0, 1, 63, 127, 128, 244, 255]
to bytes: [0, 1, 63, 127, -128, -12, -1]
to positive ints: [0, 1, 63, 127, 128, 244, 255]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[5]:

[To see links please register here]

[6]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through