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 UInt.toByteArray(
return order.normalize(bytes)
}
fun ByteArray.toUInt(
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toUByte()
}
}
fun Collection<Byte>.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
return toByteArray()
.toUInt(order)
}
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toUInt(order) }
.toUIntArray()
}
fun Collection<Byte>.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
return toByteArray()
.toUIntArray(order)
return order.normalize(bytes)
}