Compare commits
2 Commits
f825b6a7ab
...
694c44d7d5
| Author | SHA1 | Date | |
|---|---|---|---|
|
694c44d7d5
|
|||
|
e4c3a0f45f
|
@@ -1,5 +1,5 @@
|
|||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.1.1"
|
version = "1.2.0"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
148
src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt
Normal file
148
src/commonMain/kotlin/com/infendro/bytearray/ByteArray.kt
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
import kotlin.experimental.and
|
||||||
|
import kotlin.experimental.or
|
||||||
|
import kotlin.experimental.xor
|
||||||
|
|
||||||
|
infix fun ByteArray.and(
|
||||||
|
that: ByteArray,
|
||||||
|
): ByteArray {
|
||||||
|
if (this.size != that.size) throw Exception()
|
||||||
|
|
||||||
|
return ByteArray(size) { i ->
|
||||||
|
this[i] and that[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun ByteArray.or(
|
||||||
|
that: ByteArray,
|
||||||
|
): ByteArray {
|
||||||
|
if (this.size != that.size) throw Exception()
|
||||||
|
|
||||||
|
return ByteArray(size) { i ->
|
||||||
|
this[i] or that[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun ByteArray.xor(
|
||||||
|
that: ByteArray,
|
||||||
|
): ByteArray {
|
||||||
|
if (this.size != that.size) throw Exception()
|
||||||
|
|
||||||
|
return ByteArray(size) { i ->
|
||||||
|
this[i] xor that[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.padStart(
|
||||||
|
length: Int,
|
||||||
|
padByte: Byte,
|
||||||
|
): ByteArray {
|
||||||
|
return buildList {
|
||||||
|
repeat(length - size) {
|
||||||
|
add(padByte)
|
||||||
|
}
|
||||||
|
addAll(this)
|
||||||
|
}.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.padEnd(
|
||||||
|
length: Int,
|
||||||
|
padByte: Byte,
|
||||||
|
): ByteArray {
|
||||||
|
return buildList {
|
||||||
|
repeat(length - size) {
|
||||||
|
add(padByte)
|
||||||
|
}
|
||||||
|
addAll(this)
|
||||||
|
}.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toInt(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): Int {
|
||||||
|
if (size != 4) throw Exception()
|
||||||
|
|
||||||
|
val bytes = order.normalize(this)
|
||||||
|
return bytes.fold(0) { acc, byte ->
|
||||||
|
(acc shl 8) or (byte.toInt() and 0xFF)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): IntArray {
|
||||||
|
if (size % 4 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(4)
|
||||||
|
.map { it.toInt(order) }
|
||||||
|
.toIntArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toLong(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toLongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): LongArray {
|
||||||
|
if (size % 8 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(8)
|
||||||
|
.map { it.toLong(order) }
|
||||||
|
.toLongArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toUInt(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toUIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): UIntArray {
|
||||||
|
if (size % 4 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(4)
|
||||||
|
.map { it.toUInt(order) }
|
||||||
|
.toUIntArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toULong(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toULongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): ULongArray {
|
||||||
|
if (size % 8 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(8)
|
||||||
|
.map { it.toULong(order) }
|
||||||
|
.toULongArray()
|
||||||
|
}
|
||||||
@@ -7,6 +7,12 @@ enum class ByteOrder {
|
|||||||
): ByteArray {
|
): ByteArray {
|
||||||
return bytes
|
return bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun normalize(
|
||||||
|
bytes: UByteArray,
|
||||||
|
): UByteArray {
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
},
|
},
|
||||||
LITTLE_ENDIAN {
|
LITTLE_ENDIAN {
|
||||||
override fun normalize(
|
override fun normalize(
|
||||||
@@ -14,9 +20,19 @@ enum class ByteOrder {
|
|||||||
): ByteArray {
|
): ByteArray {
|
||||||
return bytes.reversedArray()
|
return bytes.reversedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun normalize(
|
||||||
|
bytes: UByteArray,
|
||||||
|
): UByteArray {
|
||||||
|
return bytes.reversedArray()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
internal abstract fun normalize(
|
internal abstract fun normalize(
|
||||||
bytes: ByteArray,
|
bytes: ByteArray,
|
||||||
): ByteArray
|
): ByteArray
|
||||||
|
|
||||||
|
internal abstract fun normalize(
|
||||||
|
bytes: UByteArray,
|
||||||
|
): UByteArray
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,75 @@
|
|||||||
package com.infendro.bytearray
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
fun MutableCollection<Byte>.addAll(
|
fun MutableCollection<Byte>.addAll(
|
||||||
bytes: ByteArray,
|
bytes: ByteArray,
|
||||||
): Boolean {
|
) = addAll(bytes.toList())
|
||||||
return addAll(bytes.toList())
|
|
||||||
}
|
fun MutableCollection<UByte>.addAll(
|
||||||
|
bytes: UByteArray,
|
||||||
|
) = addAll(bytes.toList())
|
||||||
|
|
||||||
|
fun Collection<Byte>.toInt(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toInt(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toInt(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toInt(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toIntArray(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toIntArray(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toLong(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toLong(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toLong(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toLong(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toLongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toLongArray(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toLongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toLongArray(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toUInt(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toUInt(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toUInt(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toUInt(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toUIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toUIntArray(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toUIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toUIntArray(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toULong(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toULong(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toULong(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toULong(order)
|
||||||
|
|
||||||
|
fun Collection<Byte>.toULongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toByteArray().toULongArray(order)
|
||||||
|
|
||||||
|
fun Collection<UByte>.toULongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) = toUByteArray().toULongArray(order)
|
||||||
|
|||||||
@@ -12,38 +12,12 @@ fun Int.toByteArray(
|
|||||||
return order.normalize(bytes)
|
return order.normalize(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.toInt(
|
fun Int.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): Int {
|
): UByteArray {
|
||||||
if (size != 4) throw Exception()
|
val bytes = UByteArray(4) { i ->
|
||||||
|
val offset = (3 - i) * 8
|
||||||
val bytes = order.normalize(this)
|
(this ushr offset).toUByte()
|
||||||
return bytes.fold(0) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toInt() and 0xFF)
|
|
||||||
}
|
}
|
||||||
}
|
return order.normalize(bytes)
|
||||||
|
|
||||||
fun Collection<Byte>.toInt(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): Int {
|
|
||||||
return toByteArray()
|
|
||||||
.toInt(order)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toIntArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): IntArray {
|
|
||||||
if (size % 4 != 0) throw Exception()
|
|
||||||
|
|
||||||
return toList()
|
|
||||||
.chunked(4)
|
|
||||||
.map { it.toInt(order) }
|
|
||||||
.toIntArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toIntArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): IntArray {
|
|
||||||
return toByteArray()
|
|
||||||
.toIntArray(order)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,38 +12,12 @@ fun Long.toByteArray(
|
|||||||
return order.normalize(bytes)
|
return order.normalize(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.toLong(
|
fun Long.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): Long {
|
): UByteArray {
|
||||||
if (size != 8) throw Exception()
|
val bytes = UByteArray(8) { i ->
|
||||||
|
val offset = (7 - i) * 8
|
||||||
val bytes = order.normalize(this)
|
(this ushr offset).toUByte()
|
||||||
return bytes.fold(0L) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toLong() and 0xFFL)
|
|
||||||
}
|
}
|
||||||
}
|
return order.normalize(bytes)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
145
src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt
Normal file
145
src/commonMain/kotlin/com/infendro/bytearray/UByteArray.kt
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
|
infix fun UByteArray.and(
|
||||||
|
that: UByteArray,
|
||||||
|
): UByteArray {
|
||||||
|
if (this.size != that.size) throw Exception()
|
||||||
|
|
||||||
|
return UByteArray(size) { i ->
|
||||||
|
this[i] and that[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun UByteArray.or(
|
||||||
|
that: UByteArray,
|
||||||
|
): UByteArray {
|
||||||
|
if (this.size != that.size) throw Exception()
|
||||||
|
|
||||||
|
return UByteArray(size) { i ->
|
||||||
|
this[i] or that[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun UByteArray.xor(
|
||||||
|
that: UByteArray,
|
||||||
|
): UByteArray {
|
||||||
|
if (this.size != that.size) throw Exception()
|
||||||
|
|
||||||
|
return UByteArray(size) { i ->
|
||||||
|
this[i] xor that[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.padStart(
|
||||||
|
length: Int,
|
||||||
|
padByte: UByte,
|
||||||
|
): UByteArray {
|
||||||
|
return buildList {
|
||||||
|
repeat(length - size) {
|
||||||
|
add(padByte)
|
||||||
|
}
|
||||||
|
addAll(this)
|
||||||
|
}.toUByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.padEnd(
|
||||||
|
length: Int,
|
||||||
|
padByte: UByte,
|
||||||
|
): UByteArray {
|
||||||
|
return buildList {
|
||||||
|
repeat(length - size) {
|
||||||
|
add(padByte)
|
||||||
|
}
|
||||||
|
addAll(this)
|
||||||
|
}.toUByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toInt(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): Int {
|
||||||
|
if (size != 4) throw Exception()
|
||||||
|
|
||||||
|
val bytes = order.normalize(this)
|
||||||
|
return bytes.fold(0) { acc, byte ->
|
||||||
|
(acc shl 8) or (byte.toInt() and 0xFF)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): IntArray {
|
||||||
|
if (size % 4 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(4)
|
||||||
|
.map { it.toInt(order) }
|
||||||
|
.toIntArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toLong(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toLongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): LongArray {
|
||||||
|
if (size % 8 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(8)
|
||||||
|
.map { it.toLong(order) }
|
||||||
|
.toLongArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toUInt(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toUIntArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): UIntArray {
|
||||||
|
if (size % 4 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(4)
|
||||||
|
.map { it.toUInt(order) }
|
||||||
|
.toUIntArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toULong(
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UByteArray.toULongArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): ULongArray {
|
||||||
|
if (size % 8 != 0) throw Exception()
|
||||||
|
|
||||||
|
return toList()
|
||||||
|
.chunked(8)
|
||||||
|
.map { it.toULong(order) }
|
||||||
|
.toULongArray()
|
||||||
|
}
|
||||||
@@ -12,38 +12,12 @@ fun UInt.toByteArray(
|
|||||||
return order.normalize(bytes)
|
return order.normalize(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.toUInt(
|
fun UInt.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UInt {
|
): UByteArray {
|
||||||
if (size != 4) throw Exception()
|
val bytes = UByteArray(4) { i ->
|
||||||
|
val offset = (3 - i) * 8
|
||||||
val bytes = order.normalize(this)
|
(this shr offset).toUByte()
|
||||||
return bytes.fold(0U) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
|
||||||
}
|
}
|
||||||
}
|
return order.normalize(bytes)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,38 +12,12 @@ fun ULong.toByteArray(
|
|||||||
return order.normalize(bytes)
|
return order.normalize(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.toULong(
|
fun ULong.toUByteArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ULong {
|
): UByteArray {
|
||||||
if (size != 8) throw Exception()
|
val bytes = UByteArray(8) { i ->
|
||||||
|
val offset = (7 - i) * 8
|
||||||
val bytes = order.normalize(this)
|
(this shr offset).toUByte()
|
||||||
return bytes.fold(0UL) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
|
||||||
}
|
}
|
||||||
}
|
return order.normalize(bytes)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
import kotlin.experimental.and
|
|
||||||
import kotlin.experimental.or
|
|
||||||
import kotlin.experimental.xor
|
|
||||||
|
|
||||||
infix fun ByteArray.and(
|
|
||||||
that: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
if (this.size != that.size) throw Exception()
|
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
|
||||||
this[i] and that[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun ByteArray.or(
|
|
||||||
that: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
if (this.size != that.size) throw Exception()
|
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
|
||||||
this[i] or that[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun ByteArray.xor(
|
|
||||||
that: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
if (this.size != that.size) throw Exception()
|
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
|
||||||
this[i] xor that[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user