From eb2cdbea736b0cf5959fe4f031c0f4a0f202c527 Mon Sep 17 00:00:00 2001 From: Infendro Date: Mon, 1 Dec 2025 16:24:35 +0100 Subject: [PATCH] implement typed array to byte array conversions --- .../com/infendro/bytearray/ByteArray.kt | 16 +++++++-------- .../kotlin/com/infendro/bytearray/Int.kt | 20 +++++++++++++++++++ .../kotlin/com/infendro/bytearray/Long.kt | 20 +++++++++++++++++++ .../com/infendro/bytearray/UByteArray.kt | 16 +++++++-------- .../kotlin/com/infendro/bytearray/UInt.kt | 20 +++++++++++++++++++ .../kotlin/com/infendro/bytearray/ULong.kt | 20 +++++++++++++++++++ 6 files changed, 96 insertions(+), 16 deletions(-) diff --git a/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt index fc8d526..1a5b611 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt @@ -76,8 +76,8 @@ fun ByteArray.toIntArray( if (size % 4 != 0) throw Exception() return toList() - .chunked(4) - .map { it.toByteArray().toInt(order) } + .chunked(4) { it.toByteArray() } + .map { it.toInt(order) } .toIntArray() } @@ -98,8 +98,8 @@ fun ByteArray.toLongArray( if (size % 8 != 0) throw Exception() return toList() - .chunked(8) - .map { it.toByteArray().toLong(order) } + .chunked(8) { it.toByteArray() } + .map { it.toLong(order) } .toLongArray() } @@ -120,8 +120,8 @@ fun ByteArray.toUIntArray( if (size % 4 != 0) throw Exception() return toList() - .chunked(4) - .map { it.toByteArray().toUInt(order) } + .chunked(4) { it.toByteArray() } + .map { it.toUInt(order) } .toUIntArray() } @@ -142,7 +142,7 @@ fun ByteArray.toULongArray( if (size % 8 != 0) throw Exception() return toList() - .chunked(8) - .map { it.toByteArray().toULong(order) } + .chunked(8) { it.toByteArray() } + .map { it.toULong(order) } .toULongArray() } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/Int.kt b/src/commonMain/kotlin/com/infendro/bytearray/Int.kt index 394a2f7..3314dff 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/Int.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/Int.kt @@ -12,6 +12,16 @@ fun Int.toByteArray( return order.normalize(bytes) } +fun IntArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + return buildList { + for (n in this@toByteArray) { + addAll(n.toByteArray(order)) + } + }.toByteArray() +} + fun Int.toUByteArray( order: ByteOrder = BIG_ENDIAN, ): UByteArray { @@ -21,3 +31,13 @@ fun Int.toUByteArray( } return order.normalize(bytes) } + +fun IntArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + return buildList { + for (n in this@toUByteArray) { + addAll(n.toUByteArray(order)) + } + }.toUByteArray() +} diff --git a/src/commonMain/kotlin/com/infendro/bytearray/Long.kt b/src/commonMain/kotlin/com/infendro/bytearray/Long.kt index 873edef..417d3a1 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/Long.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/Long.kt @@ -12,6 +12,16 @@ fun Long.toByteArray( return order.normalize(bytes) } +fun LongArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + return buildList { + for (n in this@toByteArray) { + addAll(n.toByteArray(order)) + } + }.toByteArray() +} + fun Long.toUByteArray( order: ByteOrder = BIG_ENDIAN, ): UByteArray { @@ -21,3 +31,13 @@ fun Long.toUByteArray( } return order.normalize(bytes) } + +fun LongArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + return buildList { + for (n in this@toUByteArray) { + addAll(n.toUByteArray(order)) + } + }.toUByteArray() +} diff --git a/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt b/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt index 0fa87f9..0591861 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt @@ -73,8 +73,8 @@ fun UByteArray.toIntArray( if (size % 4 != 0) throw Exception() return toList() - .chunked(4) - .map { it.toUByteArray().toInt(order) } + .chunked(4) { it.toUByteArray() } + .map { it.toInt(order) } .toIntArray() } @@ -95,8 +95,8 @@ fun UByteArray.toLongArray( if (size % 8 != 0) throw Exception() return toList() - .chunked(8) - .map { it.toUByteArray().toLong(order) } + .chunked(8) { it.toUByteArray() } + .map { it.toLong(order) } .toLongArray() } @@ -117,8 +117,8 @@ fun UByteArray.toUIntArray( if (size % 4 != 0) throw Exception() return toList() - .chunked(4) - .map { it.toUByteArray().toUInt(order) } + .chunked(4) { it.toUByteArray() } + .map { it.toUInt(order) } .toUIntArray() } @@ -139,7 +139,7 @@ fun UByteArray.toULongArray( if (size % 8 != 0) throw Exception() return toList() - .chunked(8) - .map { it.toUByteArray().toULong(order) } + .chunked(8) { it.toUByteArray() } + .map { it.toULong(order) } .toULongArray() } diff --git a/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt b/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt index 371971c..f7f672d 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/UInt.kt @@ -12,6 +12,16 @@ fun UInt.toByteArray( return order.normalize(bytes) } +fun UIntArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + return buildList { + for (n in this@toByteArray) { + addAll(n.toByteArray(order)) + } + }.toByteArray() +} + fun UInt.toUByteArray( order: ByteOrder = BIG_ENDIAN, ): UByteArray { @@ -21,3 +31,13 @@ fun UInt.toUByteArray( } return order.normalize(bytes) } + +fun UIntArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + return buildList { + for (n in this@toUByteArray) { + addAll(n.toUByteArray(order)) + } + }.toUByteArray() +} diff --git a/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt b/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt index ba77ef7..f23a404 100644 --- a/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt +++ b/src/commonMain/kotlin/com/infendro/bytearray/ULong.kt @@ -12,6 +12,16 @@ fun ULong.toByteArray( return order.normalize(bytes) } +fun ULongArray.toByteArray( + order: ByteOrder = BIG_ENDIAN, +): ByteArray { + return buildList { + for (n in this@toByteArray) { + addAll(n.toByteArray(order)) + } + }.toByteArray() +} + fun ULong.toUByteArray( order: ByteOrder = BIG_ENDIAN, ): UByteArray { @@ -21,3 +31,13 @@ fun ULong.toUByteArray( } return order.normalize(bytes) } + +fun ULongArray.toUByteArray( + order: ByteOrder = BIG_ENDIAN, +): UByteArray { + return buildList { + for (n in this@toUByteArray) { + addAll(n.toUByteArray(order)) + } + }.toUByteArray() +}