Compare commits
2 Commits
be9a8a4369
...
22cfdd90ab
| Author | SHA1 | Date | |
|---|---|---|---|
|
22cfdd90ab
|
|||
|
01327ef4b2
|
@@ -8,7 +8,8 @@ import kotlin.experimental.xor
|
|||||||
infix fun ByteArray.and(
|
infix fun ByteArray.and(
|
||||||
that: ByteArray,
|
that: ByteArray,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
if (this.size != that.size) throw Exception()
|
if (this.size != that.size)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
return ByteArray(size) { i ->
|
||||||
this[i] and that[i]
|
this[i] and that[i]
|
||||||
@@ -18,7 +19,8 @@ infix fun ByteArray.and(
|
|||||||
infix fun ByteArray.or(
|
infix fun ByteArray.or(
|
||||||
that: ByteArray,
|
that: ByteArray,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
if (this.size != that.size) throw Exception()
|
if (this.size != that.size)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
return ByteArray(size) { i ->
|
||||||
this[i] or that[i]
|
this[i] or that[i]
|
||||||
@@ -28,7 +30,8 @@ infix fun ByteArray.or(
|
|||||||
infix fun ByteArray.xor(
|
infix fun ByteArray.xor(
|
||||||
that: ByteArray,
|
that: ByteArray,
|
||||||
): ByteArray {
|
): ByteArray {
|
||||||
if (this.size != that.size) throw Exception()
|
if (this.size != that.size)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
return ByteArray(size) { i ->
|
||||||
this[i] xor that[i]
|
this[i] xor that[i]
|
||||||
@@ -62,10 +65,11 @@ fun ByteArray.padEnd(
|
|||||||
fun ByteArray.toInt(
|
fun ByteArray.toInt(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): Int {
|
): Int {
|
||||||
if (size != 4) throw Exception()
|
if (size != 4)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0) { acc, byte ->
|
.fold(0) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toInt() and 0xFF)
|
(acc shl 8) or (byte.toInt() and 0xFF)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,7 +77,8 @@ fun ByteArray.toInt(
|
|||||||
fun ByteArray.toIntArray(
|
fun ByteArray.toIntArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): IntArray {
|
): IntArray {
|
||||||
if (size % 4 != 0) throw Exception()
|
if (size % 4 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return toList()
|
return toList()
|
||||||
.chunked(4) { it.toByteArray() }
|
.chunked(4) { it.toByteArray() }
|
||||||
@@ -84,10 +89,11 @@ fun ByteArray.toIntArray(
|
|||||||
fun ByteArray.toLong(
|
fun ByteArray.toLong(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): Long {
|
): Long {
|
||||||
if (size != 8) throw Exception()
|
if (size != 8)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0L) { acc, byte ->
|
.fold(0L) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toLong() and 0xFFL)
|
(acc shl 8) or (byte.toLong() and 0xFFL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,7 +101,8 @@ fun ByteArray.toLong(
|
|||||||
fun ByteArray.toLongArray(
|
fun ByteArray.toLongArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): LongArray {
|
): LongArray {
|
||||||
if (size % 8 != 0) throw Exception()
|
if (size % 8 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return toList()
|
return toList()
|
||||||
.chunked(8) { it.toByteArray() }
|
.chunked(8) { it.toByteArray() }
|
||||||
@@ -106,10 +113,11 @@ fun ByteArray.toLongArray(
|
|||||||
fun ByteArray.toUInt(
|
fun ByteArray.toUInt(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UInt {
|
): UInt {
|
||||||
if (size != 4) throw Exception()
|
if (size != 4)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0U) { acc, byte ->
|
.fold(0U) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,7 +125,8 @@ fun ByteArray.toUInt(
|
|||||||
fun ByteArray.toUIntArray(
|
fun ByteArray.toUIntArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UIntArray {
|
): UIntArray {
|
||||||
if (size % 4 != 0) throw Exception()
|
if (size % 4 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return toList()
|
return toList()
|
||||||
.chunked(4) { it.toByteArray() }
|
.chunked(4) { it.toByteArray() }
|
||||||
@@ -128,10 +137,11 @@ fun ByteArray.toUIntArray(
|
|||||||
fun ByteArray.toULong(
|
fun ByteArray.toULong(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ULong {
|
): ULong {
|
||||||
if (size != 8) throw Exception()
|
if (size != 8)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0UL) { acc, byte ->
|
.fold(0UL) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,7 +149,8 @@ fun ByteArray.toULong(
|
|||||||
fun ByteArray.toULongArray(
|
fun ByteArray.toULongArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ULongArray {
|
): ULongArray {
|
||||||
if (size % 8 != 0) throw Exception()
|
if (size % 8 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return toList()
|
return toList()
|
||||||
.chunked(8) { it.toByteArray() }
|
.chunked(8) { it.toByteArray() }
|
||||||
|
|||||||
@@ -1,38 +1,22 @@
|
|||||||
package com.infendro.bytearray
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
|
||||||
|
|
||||||
enum class ByteOrder {
|
enum class ByteOrder {
|
||||||
BIG_ENDIAN {
|
BIG_ENDIAN, LITTLE_ENDIAN;
|
||||||
override fun normalize(
|
}
|
||||||
bytes: ByteArray,
|
|
||||||
): ByteArray {
|
fun ByteArray.normalize(
|
||||||
return bytes
|
order: ByteOrder,
|
||||||
}
|
): ByteArray = when (order) {
|
||||||
|
BIG_ENDIAN -> this
|
||||||
override fun normalize(
|
LITTLE_ENDIAN -> reversedArray()
|
||||||
bytes: UByteArray,
|
}
|
||||||
): UByteArray {
|
|
||||||
return bytes
|
fun UByteArray.normalize(
|
||||||
}
|
order: ByteOrder,
|
||||||
},
|
): UByteArray = when (order) {
|
||||||
LITTLE_ENDIAN {
|
BIG_ENDIAN -> this
|
||||||
override fun normalize(
|
LITTLE_ENDIAN -> reversedArray()
|
||||||
bytes: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
return bytes.reversedArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun normalize(
|
|
||||||
bytes: UByteArray,
|
|
||||||
): UByteArray {
|
|
||||||
return bytes.reversedArray()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
internal abstract fun normalize(
|
|
||||||
bytes: ByteArray,
|
|
||||||
): ByteArray
|
|
||||||
|
|
||||||
internal abstract fun normalize(
|
|
||||||
bytes: UByteArray,
|
|
||||||
): UByteArray
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,17 +9,7 @@ fun Int.toByteArray(
|
|||||||
val offset = (3 - i) * 8
|
val offset = (3 - i) * 8
|
||||||
(this ushr offset).toByte()
|
(this ushr offset).toByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun IntArray.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = mutableListOf<Byte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toByteArray(order).toList()
|
|
||||||
}
|
|
||||||
return bytes.toByteArray()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Int.toUByteArray(
|
fun Int.toUByteArray(
|
||||||
@@ -29,15 +19,5 @@ fun Int.toUByteArray(
|
|||||||
val offset = (3 - i) * 8
|
val offset = (3 - i) * 8
|
||||||
(this ushr offset).toUByte()
|
(this ushr offset).toUByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun IntArray.toUByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UByteArray {
|
|
||||||
val bytes = mutableListOf<UByte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toUByteArray(order)
|
|
||||||
}
|
|
||||||
return bytes.toUByteArray()
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/commonMain/kotlin/com/infendro/bytearray/IntArray.kt
Normal file
23
src/commonMain/kotlin/com/infendro/bytearray/IntArray.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
|
fun IntArray.toByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): ByteArray {
|
||||||
|
val bytes = mutableListOf<Byte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toByteArray(order).toList()
|
||||||
|
}
|
||||||
|
return bytes.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun IntArray.toUByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): UByteArray {
|
||||||
|
val bytes = mutableListOf<UByte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toUByteArray(order)
|
||||||
|
}
|
||||||
|
return bytes.toUByteArray()
|
||||||
|
}
|
||||||
@@ -9,17 +9,7 @@ fun Long.toByteArray(
|
|||||||
val offset = (7 - i) * 8
|
val offset = (7 - i) * 8
|
||||||
(this ushr offset).toByte()
|
(this ushr offset).toByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun LongArray.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = mutableListOf<Byte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toByteArray(order).toList()
|
|
||||||
}
|
|
||||||
return bytes.toByteArray()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Long.toUByteArray(
|
fun Long.toUByteArray(
|
||||||
@@ -29,15 +19,5 @@ fun Long.toUByteArray(
|
|||||||
val offset = (7 - i) * 8
|
val offset = (7 - i) * 8
|
||||||
(this ushr offset).toUByte()
|
(this ushr offset).toUByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun LongArray.toUByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UByteArray {
|
|
||||||
val bytes = mutableListOf<UByte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toUByteArray(order)
|
|
||||||
}
|
|
||||||
return bytes.toUByteArray()
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/commonMain/kotlin/com/infendro/bytearray/LongArray.kt
Normal file
23
src/commonMain/kotlin/com/infendro/bytearray/LongArray.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
|
fun LongArray.toByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): ByteArray {
|
||||||
|
val bytes = mutableListOf<Byte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toByteArray(order).toList()
|
||||||
|
}
|
||||||
|
return bytes.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun LongArray.toUByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): UByteArray {
|
||||||
|
val bytes = mutableListOf<UByte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toUByteArray(order)
|
||||||
|
}
|
||||||
|
return bytes.toUByteArray()
|
||||||
|
}
|
||||||
@@ -2,3 +2,6 @@ package com.infendro.bytearray
|
|||||||
|
|
||||||
fun String.encodeToUByteArray(): UByteArray =
|
fun String.encodeToUByteArray(): UByteArray =
|
||||||
encodeToByteArray().toUByteArray()
|
encodeToByteArray().toUByteArray()
|
||||||
|
|
||||||
|
fun UByteArray.decodeToString(): String =
|
||||||
|
toByteArray().decodeToString()
|
||||||
|
|||||||
@@ -2,13 +2,11 @@ package com.infendro.bytearray
|
|||||||
|
|
||||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
fun UByteArray.decodeToString(): String =
|
|
||||||
toByteArray().decodeToString()
|
|
||||||
|
|
||||||
infix fun UByteArray.and(
|
infix fun UByteArray.and(
|
||||||
that: UByteArray,
|
that: UByteArray,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
if (this.size != that.size) throw Exception()
|
if (this.size != that.size)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return UByteArray(size) { i ->
|
return UByteArray(size) { i ->
|
||||||
this[i] and that[i]
|
this[i] and that[i]
|
||||||
@@ -18,7 +16,8 @@ infix fun UByteArray.and(
|
|||||||
infix fun UByteArray.or(
|
infix fun UByteArray.or(
|
||||||
that: UByteArray,
|
that: UByteArray,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
if (this.size != that.size) throw Exception()
|
if (this.size != that.size)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return UByteArray(size) { i ->
|
return UByteArray(size) { i ->
|
||||||
this[i] or that[i]
|
this[i] or that[i]
|
||||||
@@ -28,7 +27,8 @@ infix fun UByteArray.or(
|
|||||||
infix fun UByteArray.xor(
|
infix fun UByteArray.xor(
|
||||||
that: UByteArray,
|
that: UByteArray,
|
||||||
): UByteArray {
|
): UByteArray {
|
||||||
if (this.size != that.size) throw Exception()
|
if (this.size != that.size)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return UByteArray(size) { i ->
|
return UByteArray(size) { i ->
|
||||||
this[i] xor that[i]
|
this[i] xor that[i]
|
||||||
@@ -62,10 +62,11 @@ fun UByteArray.padEnd(
|
|||||||
fun UByteArray.toInt(
|
fun UByteArray.toInt(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): Int {
|
): Int {
|
||||||
if (size != 4) throw Exception()
|
if (size != 4)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0) { acc, byte ->
|
.fold(0) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toInt() and 0xFF)
|
(acc shl 8) or (byte.toInt() and 0xFF)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,7 +74,8 @@ fun UByteArray.toInt(
|
|||||||
fun UByteArray.toIntArray(
|
fun UByteArray.toIntArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): IntArray {
|
): IntArray {
|
||||||
if (size % 4 != 0) throw Exception()
|
if (size % 4 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return chunked(4) { it.toUByteArray() }
|
return chunked(4) { it.toUByteArray() }
|
||||||
.map { it.toInt(order) }
|
.map { it.toInt(order) }
|
||||||
@@ -83,10 +85,11 @@ fun UByteArray.toIntArray(
|
|||||||
fun UByteArray.toLong(
|
fun UByteArray.toLong(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): Long {
|
): Long {
|
||||||
if (size != 8) throw Exception()
|
if (size != 8)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0L) { acc, byte ->
|
.fold(0L) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toLong() and 0xFFL)
|
(acc shl 8) or (byte.toLong() and 0xFFL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,7 +97,8 @@ fun UByteArray.toLong(
|
|||||||
fun UByteArray.toLongArray(
|
fun UByteArray.toLongArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): LongArray {
|
): LongArray {
|
||||||
if (size % 8 != 0) throw Exception()
|
if (size % 8 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return chunked(8) { it.toUByteArray() }
|
return chunked(8) { it.toUByteArray() }
|
||||||
.map { it.toLong(order) }
|
.map { it.toLong(order) }
|
||||||
@@ -104,10 +108,11 @@ fun UByteArray.toLongArray(
|
|||||||
fun UByteArray.toUInt(
|
fun UByteArray.toUInt(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UInt {
|
): UInt {
|
||||||
if (size != 4) throw Exception()
|
if (size != 4)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0U) { acc, byte ->
|
.fold(0U) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,7 +120,8 @@ fun UByteArray.toUInt(
|
|||||||
fun UByteArray.toUIntArray(
|
fun UByteArray.toUIntArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): UIntArray {
|
): UIntArray {
|
||||||
if (size % 4 != 0) throw Exception()
|
if (size % 4 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return chunked(4) { it.toUByteArray() }
|
return chunked(4) { it.toUByteArray() }
|
||||||
.map { it.toUInt(order) }
|
.map { it.toUInt(order) }
|
||||||
@@ -125,10 +131,11 @@ fun UByteArray.toUIntArray(
|
|||||||
fun UByteArray.toULong(
|
fun UByteArray.toULong(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ULong {
|
): ULong {
|
||||||
if (size != 8) throw Exception()
|
if (size != 8)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
return normalize(order)
|
||||||
return bytes.fold(0UL) { acc, byte ->
|
.fold(0UL) { acc, byte ->
|
||||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,7 +143,8 @@ fun UByteArray.toULong(
|
|||||||
fun UByteArray.toULongArray(
|
fun UByteArray.toULongArray(
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
): ULongArray {
|
): ULongArray {
|
||||||
if (size % 8 != 0) throw Exception()
|
if (size % 8 != 0)
|
||||||
|
throw IllegalArgumentException()
|
||||||
|
|
||||||
return chunked(8) { it.toUByteArray() }
|
return chunked(8) { it.toUByteArray() }
|
||||||
.map { it.toULong(order) }
|
.map { it.toULong(order) }
|
||||||
|
|||||||
@@ -9,17 +9,7 @@ fun UInt.toByteArray(
|
|||||||
val offset = (3 - i) * 8
|
val offset = (3 - i) * 8
|
||||||
(this shr offset).toByte()
|
(this shr offset).toByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun UIntArray.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = mutableListOf<Byte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toByteArray(order).toList()
|
|
||||||
}
|
|
||||||
return bytes.toByteArray()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun UInt.toUByteArray(
|
fun UInt.toUByteArray(
|
||||||
@@ -29,15 +19,5 @@ fun UInt.toUByteArray(
|
|||||||
val offset = (3 - i) * 8
|
val offset = (3 - i) * 8
|
||||||
(this shr offset).toUByte()
|
(this shr offset).toUByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun UIntArray.toUByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UByteArray {
|
|
||||||
val bytes = mutableListOf<UByte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toUByteArray(order)
|
|
||||||
}
|
|
||||||
return bytes.toUByteArray()
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/commonMain/kotlin/com/infendro/bytearray/UIntArray.kt
Normal file
23
src/commonMain/kotlin/com/infendro/bytearray/UIntArray.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
|
fun UIntArray.toByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): ByteArray {
|
||||||
|
val bytes = mutableListOf<Byte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toByteArray(order).toList()
|
||||||
|
}
|
||||||
|
return bytes.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun UIntArray.toUByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): UByteArray {
|
||||||
|
val bytes = mutableListOf<UByte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toUByteArray(order)
|
||||||
|
}
|
||||||
|
return bytes.toUByteArray()
|
||||||
|
}
|
||||||
@@ -9,17 +9,7 @@ fun ULong.toByteArray(
|
|||||||
val offset = (7 - i) * 8
|
val offset = (7 - i) * 8
|
||||||
(this shr offset).toByte()
|
(this shr offset).toByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun ULongArray.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = mutableListOf<Byte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toByteArray(order).toList()
|
|
||||||
}
|
|
||||||
return bytes.toByteArray()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ULong.toUByteArray(
|
fun ULong.toUByteArray(
|
||||||
@@ -29,15 +19,5 @@ fun ULong.toUByteArray(
|
|||||||
val offset = (7 - i) * 8
|
val offset = (7 - i) * 8
|
||||||
(this shr offset).toUByte()
|
(this shr offset).toUByte()
|
||||||
}
|
}
|
||||||
return order.normalize(bytes)
|
return bytes.normalize(order)
|
||||||
}
|
|
||||||
|
|
||||||
fun ULongArray.toUByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UByteArray {
|
|
||||||
val bytes = mutableListOf<UByte>()
|
|
||||||
for (n in this) {
|
|
||||||
bytes += n.toUByteArray(order)
|
|
||||||
}
|
|
||||||
return bytes.toUByteArray()
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/commonMain/kotlin/com/infendro/bytearray/ULongArray.kt
Normal file
23
src/commonMain/kotlin/com/infendro/bytearray/ULongArray.kt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
||||||
|
|
||||||
|
fun ULongArray.toByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): ByteArray {
|
||||||
|
val bytes = mutableListOf<Byte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toByteArray(order).toList()
|
||||||
|
}
|
||||||
|
return bytes.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ULongArray.toUByteArray(
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
): UByteArray {
|
||||||
|
val bytes = mutableListOf<UByte>()
|
||||||
|
for (n in this) {
|
||||||
|
bytes += n.toUByteArray(order)
|
||||||
|
}
|
||||||
|
return bytes.toUByteArray()
|
||||||
|
}
|
||||||
@@ -17,71 +17,188 @@ class `ByteArray Test` {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `and()`() {
|
||||||
|
val expected = byteArrayOf(0x18, 0x00)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `or() - size`() {
|
||||||
|
assertFails {
|
||||||
|
byteArrayOf() or byteArrayOf(0x00)
|
||||||
|
}
|
||||||
|
assertFails {
|
||||||
|
byteArrayOf(0x00) or byteArrayOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `or()`() {
|
||||||
|
val expected = byteArrayOf(0x7E, 0x7F)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `xor() - size`() {
|
||||||
|
assertFails {
|
||||||
|
byteArrayOf() xor byteArrayOf(0x00)
|
||||||
|
}
|
||||||
|
assertFails {
|
||||||
|
byteArrayOf(0x00) xor byteArrayOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `xor()`() {
|
||||||
|
val expected = byteArrayOf(0x66, 0x7F)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `padStart()`() {
|
fun `padStart()`() {
|
||||||
val padded = byteArrayOf(0x02, 0x02).padStart(8, 0x01)
|
val expected = byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02)
|
||||||
assertEquals(8, padded.size)
|
val actual = byteArrayOf(0x02, 0x02).padStart(8, 0x01)
|
||||||
assertContentEquals(
|
assertEquals(8, actual.size)
|
||||||
byteArrayOf(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02),
|
assertContentEquals(expected, actual)
|
||||||
padded
|
}
|
||||||
)
|
|
||||||
|
@Test
|
||||||
|
fun `padStart() - unchanged`() {
|
||||||
|
val expected = byteArrayOf(0x02, 0x02)
|
||||||
|
val actual = byteArrayOf(0x02, 0x02).padStart(2, 0x01)
|
||||||
|
assertEquals(2, actual.size)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `padEnd()`() {
|
fun `padEnd()`() {
|
||||||
val padded = byteArrayOf(0x02, 0x02).padEnd(8, 0x01)
|
val expected = byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01)
|
||||||
assertEquals(8, padded.size)
|
val actual = byteArrayOf(0x02, 0x02).padEnd(8, 0x01)
|
||||||
assertContentEquals(
|
assertEquals(8, actual.size)
|
||||||
byteArrayOf(0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01),
|
assertContentEquals(expected, actual)
|
||||||
padded
|
}
|
||||||
)
|
|
||||||
|
@Test
|
||||||
|
fun `padEnd() - unchanged`() {
|
||||||
|
val expected = byteArrayOf(0x02, 0x02)
|
||||||
|
val actual = byteArrayOf(0x02, 0x02).padEnd(2, 0x01)
|
||||||
|
assertEquals(2, actual.size)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toInt()`() {
|
fun `toInt()`() {
|
||||||
assertEquals(
|
val expected = 0x12345678
|
||||||
0x12345678,
|
val actual = arrayOf(
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt()
|
byteArrayOf(0x12, 0x34, 0x56, 0x78).toInt(),
|
||||||
|
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x12345678,
|
assertEquals(expected, actual[1])
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12).toInt(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toIntArray()`() {
|
||||||
|
val expected = intArrayOf(0x12345678, 0x12345678)
|
||||||
|
val actual = arrayOf(
|
||||||
|
byteArrayOf(
|
||||||
|
0x12, 0x34, 0x56, 0x78,
|
||||||
|
0x12, 0x34, 0x56, 0x78,
|
||||||
|
).toIntArray(),
|
||||||
|
byteArrayOf(
|
||||||
|
0x78, 0x56, 0x34, 0x12,
|
||||||
|
0x78, 0x56, 0x34, 0x12,
|
||||||
|
).toIntArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toLong()`() {
|
fun `toLong()`() {
|
||||||
assertEquals(
|
val expected = 0x1234567812345678L
|
||||||
0x1234567812345678L,
|
val actual = arrayOf(
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong()
|
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toLong(),
|
||||||
|
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x1234567812345678L,
|
assertEquals(expected, actual[1])
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toLong(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toLongArray()`() {
|
||||||
|
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
|
||||||
|
val actual = arrayOf(
|
||||||
|
byteArrayOf(
|
||||||
|
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||||
|
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||||
|
).toLongArray(),
|
||||||
|
byteArrayOf(
|
||||||
|
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||||
|
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||||
|
).toLongArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toUInt()`() {
|
fun `toUInt()`() {
|
||||||
assertEquals(
|
val expected = 0x12345678U
|
||||||
0x12345678U,
|
val actual = arrayOf(
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt()
|
byteArrayOf(0x12, 0x34, 0x56, 0x78).toUInt(),
|
||||||
|
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x12345678U,
|
assertEquals(expected, actual[1])
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12).toUInt(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toUIntArray()`() {
|
||||||
|
val expected = uintArrayOf(0x12345678U, 0x12345678U)
|
||||||
|
val actual = arrayOf(
|
||||||
|
byteArrayOf(
|
||||||
|
0x12, 0x34, 0x56, 0x78,
|
||||||
|
0x12, 0x34, 0x56, 0x78,
|
||||||
|
).toUIntArray(),
|
||||||
|
byteArrayOf(
|
||||||
|
0x78, 0x56, 0x34, 0x12,
|
||||||
|
0x78, 0x56, 0x34, 0x12,
|
||||||
|
).toUIntArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toULong()`() {
|
fun `toULong()`() {
|
||||||
assertEquals(
|
val expected = 0x1234567812345678UL
|
||||||
0x1234567812345678UL,
|
val actual = arrayOf(
|
||||||
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong()
|
byteArrayOf(0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78).toULong(),
|
||||||
|
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x1234567812345678UL,
|
assertEquals(expected, actual[1])
|
||||||
byteArrayOf(0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12).toULong(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toULongArray()`() {
|
||||||
|
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
|
||||||
|
val actual = arrayOf(
|
||||||
|
byteArrayOf(
|
||||||
|
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||||
|
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78,
|
||||||
|
).toULongArray(),
|
||||||
|
byteArrayOf(
|
||||||
|
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||||
|
0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12,
|
||||||
|
).toULongArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/commonTest/kotlin/com/infendro/bytearray/String.kt
Normal file
21
src/commonTest/kotlin/com/infendro/bytearray/String.kt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package com.infendro.bytearray
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class `String Test` {
|
||||||
|
@Test
|
||||||
|
fun `encodeToUByteArray()`() {
|
||||||
|
val expected = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U)
|
||||||
|
val actual = "PASSWORD".encodeToUByteArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `decodeToString()`() {
|
||||||
|
val expected = "PASSWORD"
|
||||||
|
val actual = ubyteArrayOf(0x50U, 0x41U, 0x53U, 0x53U, 0x57U, 0x4FU, 0x52U, 0x44U).decodeToString()
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,71 +17,188 @@ class `UByteArray Test` {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `and()`() {
|
||||||
|
val expected = ubyteArrayOf(0x18U, 0x00U)
|
||||||
|
val actual = ubyteArrayOf(0x5CU, 0x70U) and ubyteArrayOf(0x3AU, 0x0FU)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `or() - size`() {
|
||||||
|
assertFails {
|
||||||
|
ubyteArrayOf() or ubyteArrayOf(0x00U)
|
||||||
|
}
|
||||||
|
assertFails {
|
||||||
|
ubyteArrayOf(0x00U) or ubyteArrayOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `or()`() {
|
||||||
|
val expected = ubyteArrayOf(0x7EU, 0x7FU)
|
||||||
|
val actual = ubyteArrayOf(0x5CU, 0x70U) or ubyteArrayOf(0x3AU, 0x0FU)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `xor() - size`() {
|
||||||
|
assertFails {
|
||||||
|
ubyteArrayOf() xor ubyteArrayOf(0x00U)
|
||||||
|
}
|
||||||
|
assertFails {
|
||||||
|
ubyteArrayOf(0x00U) xor ubyteArrayOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `xor()`() {
|
||||||
|
val expected = ubyteArrayOf(0x66U, 0x7FU)
|
||||||
|
val actual = ubyteArrayOf(0x5CU, 0x70U) xor ubyteArrayOf(0x3AU, 0x0FU)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `padStart()`() {
|
fun `padStart()`() {
|
||||||
val padded = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U)
|
val expected = ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U)
|
||||||
assertEquals(8, padded.size)
|
val actual = ubyteArrayOf(0x02U, 0x02U).padStart(8, 0x01U)
|
||||||
assertContentEquals(
|
assertEquals(8, actual.size)
|
||||||
ubyteArrayOf(0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x02U, 0x02U),
|
assertContentEquals(expected, actual)
|
||||||
padded
|
}
|
||||||
)
|
|
||||||
|
@Test
|
||||||
|
fun `padStart() - unchanged`() {
|
||||||
|
val expected = ubyteArrayOf(0x02U, 0x02U)
|
||||||
|
val actual = ubyteArrayOf(0x02U, 0x02U).padStart(2, 0x01U)
|
||||||
|
assertEquals(2, actual.size)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `padEnd()`() {
|
fun `padEnd()`() {
|
||||||
val padded = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U)
|
val expected = ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U)
|
||||||
assertEquals(8, padded.size)
|
val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(8, 0x01U)
|
||||||
assertContentEquals(
|
assertEquals(8, actual.size)
|
||||||
ubyteArrayOf(0x02U, 0x02U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U),
|
assertContentEquals(expected, actual)
|
||||||
padded
|
}
|
||||||
)
|
|
||||||
|
@Test
|
||||||
|
fun `padEnd() - unchanged`() {
|
||||||
|
val expected = ubyteArrayOf(0x02U, 0x02U)
|
||||||
|
val actual = ubyteArrayOf(0x02U, 0x02U).padEnd(2, 0x01U)
|
||||||
|
assertEquals(2, actual.size)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toInt()`() {
|
fun `toInt()`() {
|
||||||
assertEquals(
|
val expected = 0x12345678
|
||||||
0x12345678,
|
val actual = arrayOf(
|
||||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt()
|
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toInt(),
|
||||||
|
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x12345678,
|
assertEquals(expected, actual[1])
|
||||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toInt(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toIntArray()`() {
|
||||||
|
val expected = intArrayOf(0x12345678, 0x12345678)
|
||||||
|
val actual = arrayOf(
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
).toIntArray(),
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
).toIntArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toLong()`() {
|
fun `toLong()`() {
|
||||||
assertEquals(
|
val expected = 0x1234567812345678L
|
||||||
0x1234567812345678L,
|
val actual = arrayOf(
|
||||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong()
|
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toLong(),
|
||||||
|
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x1234567812345678L,
|
assertEquals(expected, actual[1])
|
||||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toLong(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toLongArray()`() {
|
||||||
|
val expected = longArrayOf(0x1234567812345678L, 0x1234567812345678L)
|
||||||
|
val actual = arrayOf(
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
).toLongArray(),
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
).toLongArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toUInt()`() {
|
fun `toUInt()`() {
|
||||||
assertEquals(
|
val expected = 0x12345678U
|
||||||
0x12345678U,
|
val actual = arrayOf(
|
||||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt()
|
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U).toUInt(),
|
||||||
|
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x12345678U,
|
assertEquals(expected, actual[1])
|
||||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U).toUInt(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toUIntArray()`() {
|
||||||
|
val expected = uintArrayOf(0x12345678U, 0x12345678U)
|
||||||
|
val actual = arrayOf(
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
).toUIntArray(),
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
).toUIntArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `toULong()`() {
|
fun `toULong()`() {
|
||||||
assertEquals(
|
val expected = 0x1234567812345678UL
|
||||||
0x1234567812345678UL,
|
val actual = arrayOf(
|
||||||
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong()
|
ubyteArrayOf(0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U).toULong(),
|
||||||
|
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
assertEquals(
|
assertEquals(expected, actual[0])
|
||||||
0x1234567812345678UL,
|
assertEquals(expected, actual[1])
|
||||||
ubyteArrayOf(0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U).toULong(LITTLE_ENDIAN)
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `toULongArray()`() {
|
||||||
|
val expected = ulongArrayOf(0x1234567812345678UL, 0x1234567812345678UL)
|
||||||
|
val actual = arrayOf(
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
0x12U, 0x34U, 0x56U, 0x78U, 0x12U, 0x34U, 0x56U, 0x78U,
|
||||||
|
).toULongArray(),
|
||||||
|
ubyteArrayOf(
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
0x78U, 0x56U, 0x34U, 0x12U, 0x78U, 0x56U, 0x34U, 0x12U,
|
||||||
|
).toULongArray(LITTLE_ENDIAN),
|
||||||
)
|
)
|
||||||
|
assertContentEquals(expected, actual[0])
|
||||||
|
assertContentEquals(expected, actual[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user