Files
bytes.kt/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt

24 lines
525 B
Kotlin

package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toByte()
}
return order.normalize(bytes)
}
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toUByte()
}
return order.normalize(bytes)
}