24 lines
527 B
Kotlin
24 lines
527 B
Kotlin
package com.infendro.bytearray
|
|
|
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
|
|
|
fun Long.toByteArray(
|
|
order: ByteOrder = BIG_ENDIAN,
|
|
): ByteArray {
|
|
val bytes = ByteArray(8) { i ->
|
|
val offset = (7 - i) * 8
|
|
(this ushr offset).toByte()
|
|
}
|
|
return order.normalize(bytes)
|
|
}
|
|
|
|
fun Long.toUByteArray(
|
|
order: ByteOrder = BIG_ENDIAN,
|
|
): UByteArray {
|
|
val bytes = UByteArray(8) { i ->
|
|
val offset = (7 - i) * 8
|
|
(this ushr offset).toUByte()
|
|
}
|
|
return order.normalize(bytes)
|
|
}
|