44 lines
973 B
Kotlin
44 lines
973 B
Kotlin
package com.infendro.bytearray
|
|
|
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
|
|
|
fun ULong.toByteArray(
|
|
order: ByteOrder = BIG_ENDIAN,
|
|
): ByteArray {
|
|
val bytes = ByteArray(8) { i ->
|
|
val offset = (7 - i) * 8
|
|
(this shr offset).toByte()
|
|
}
|
|
return order.normalize(bytes)
|
|
}
|
|
|
|
fun ULongArray.toByteArray(
|
|
order: ByteOrder = BIG_ENDIAN,
|
|
): ByteArray {
|
|
val bytes = mutableListOf<Byte>()
|
|
for (n in this) {
|
|
bytes += n.toByteArray(order).toList()
|
|
}
|
|
return bytes.toByteArray()
|
|
}
|
|
|
|
fun ULong.toUByteArray(
|
|
order: ByteOrder = BIG_ENDIAN,
|
|
): UByteArray {
|
|
val bytes = UByteArray(8) { i ->
|
|
val offset = (7 - i) * 8
|
|
(this shr offset).toUByte()
|
|
}
|
|
return order.normalize(bytes)
|
|
}
|
|
|
|
fun ULongArray.toUByteArray(
|
|
order: ByteOrder = BIG_ENDIAN,
|
|
): UByteArray {
|
|
val bytes = mutableListOf<UByte>()
|
|
for (n in this) {
|
|
bytes += n.toUByteArray(order)
|
|
}
|
|
return bytes.toUByteArray()
|
|
}
|