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 Long.toByteArray(
return order.normalize(bytes)
}
fun ByteArray.toLong(
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toUByte()
}
}
fun Collection<Byte>.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
return toByteArray()
.toLong(order)
}
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toLong(order) }
.toLongArray()
}
fun Collection<Byte>.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
return toByteArray()
.toLongArray(order)
return order.normalize(bytes)
}