Stefan Oltmann
09/28/2022, 3:10 PMprivate fun getUInt16(firstByte: Byte, secondByte: Byte): Int =
firstByte.toInt() shl 8 and 0xFF00 or (secondByte.toInt() and 0xFF)
Raphael TEYSSANDIER
09/28/2022, 3:13 PMRaphael TEYSSANDIER
09/28/2022, 3:14 PMprivate infix fun Byte.getUInt16(byte: Byte): Int =
toInt() shl 8 and 0xFF00 or (byte.toInt() and 0xFF)
Klitos Kyriacou
09/28/2022, 3:42 PMprivate fun getUInt16(firstByte: Byte, secondByte: Byte): Int =
firstByte.toUByte().toInt() shl 8 or secondByte.toUByte().toInt()
... though if your function is called getUInt16, maybe it should return a UInt instead of an Int; then the expression becomes slightly simpler:
private fun getUInt16(firstByte: Byte, secondByte: Byte): UInt =
firstByte.toUByte() * 256u + secondByte.toUByte()
(though I've had to use arithmetic operations because Kotlin doesn't allow bitwise operations on UByte for some strange reason)ephemient
09/28/2022, 4:39 PMStefan Oltmann
09/30/2022, 6:30 AMconst val INT16_BYTE_SIZE: Int = 2
const val INT24_BYTE_SIZE: Int = 3
const val INT32_BYTE_SIZE: Int = 4
fun Byte.toInt8(): Byte = this
fun Byte.toUInt8(): Short = (toInt() and 0xFF).toShort()
fun ByteArray.toInt16(): Short {
require(size == INT16_BYTE_SIZE) { "Required $INT16_BYTE_SIZE bytes, but was $size" }
return (this[0].toShort().toInt() shl 8 and 0xFF00.toShort().toInt() or
(this[1].toShort().toInt() and 0xFF.toShort().toInt())).toShort()
}
fun ByteArray.toUInt16(): Int {
require(size == INT16_BYTE_SIZE) { "Required $INT16_BYTE_SIZE bytes, but was $size" }
return this[0].toInt() shl 8 and 0xFF00 or (this[1].toInt() and 0xFF)
}
fun ByteArray.toInt24(): Int {
require(size == INT24_BYTE_SIZE) { "Required $INT24_BYTE_SIZE bytes, but was $size" }
return this[0].toInt() shl 16 and 0xFF0000 or
(this[1].toInt() shl 8 and 0xFF00) or
(this[2].toInt() and 0xFF)
}
fun ByteArray.toInt32(): Int {
require(size == INT32_BYTE_SIZE) { "Required $INT32_BYTE_SIZE bytes, but was $size" }
return this[0].toInt() shl 24 and -0x1000000 or
(this[1].toInt() shl 16 and 0xFF0000) or
(this[2].toInt() shl 8 and 0xFF00) or
(this[3].toInt() and 0xFF)
}
fun ByteArray.toUInt32(): Long {
require(size == INT32_BYTE_SIZE) { "Required $INT32_BYTE_SIZE bytes, but was $size" }
return this[0].toLong() shl 24 and 0xFF000000L or
(this[1].toLong() shl 16 and 0xFF0000L) or
(this[2].toLong() shl 8 and 0xFF00L) or
(this[3].toLong() and 0xFFL)
}
Klitos Kyriacou
09/30/2022, 8:05 AMthis[0].toShort().toInt()
- isn't that the same as this[0].toInt()
?Klitos Kyriacou
09/30/2022, 8:05 AMByteBuffer.getInt()
etc.Stefan Oltmann
09/30/2022, 8:09 AMStefan Oltmann
09/30/2022, 8:12 AMtoInt16
can also be written like (this[0].toInt() shl 8 and 0xFF00 or (this[1].toInt() and 0xFF)).toShort()
with the same output.Klitos Kyriacou
09/30/2022, 8:16 AMfun ByteArray.toInt32() = ByteBuffer.wrap(this).getInt()
Stefan Oltmann
09/30/2022, 8:19 AMByteBuffer
is a JVM class, right? I should have mentioned that I'm on Kotlin/Native for Multiplatform. 🙂Klitos Kyriacou
09/30/2022, 8:21 AMephemient
09/30/2022, 1:19 PMStefan Oltmann
09/30/2022, 1:22 PM