implement padding, support UByteArray

This commit is contained in:
2025-11-10 13:51:21 +01:00
parent f825b6a7ab
commit e4c3a0f45f
9 changed files with 404 additions and 166 deletions

View File

@@ -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)
}