implement padding, support UByteArray
This commit is contained in:
@@ -12,38 +12,12 @@ fun ULong.toByteArray(
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
fun ByteArray.toULong(
|
||||
fun ULong.toUByteArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong {
|
||||
if (size != 8) throw Exception()
|
||||
|
||||
val bytes = order.normalize(this)
|
||||
return bytes.fold(0UL) { acc, byte ->
|
||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
||||
): UByteArray {
|
||||
val bytes = UByteArray(8) { i ->
|
||||
val offset = (7 - i) * 8
|
||||
(this shr offset).toUByte()
|
||||
}
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toULong(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULong {
|
||||
return toByteArray()
|
||||
.toULong(order)
|
||||
}
|
||||
|
||||
fun ByteArray.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray {
|
||||
if (size % 8 != 0) throw Exception()
|
||||
|
||||
return toList()
|
||||
.chunked(8)
|
||||
.map { it.toULong(order) }
|
||||
.toULongArray()
|
||||
}
|
||||
|
||||
fun Collection<Byte>.toULongArray(
|
||||
order: ByteOrder = BIG_ENDIAN,
|
||||
): ULongArray {
|
||||
return toByteArray()
|
||||
.toULongArray(order)
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user