implement padding, support UByteArray
This commit is contained in:
@@ -12,38 +12,12 @@ fun Int.toByteArray(
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
fun ByteArray.toInt(
|
||||
fun Int.toUByteArray(
|
||||
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)
|
||||
): UByteArray {
|
||||
val bytes = UByteArray(4) { i ->
|
||||
val offset = (3 - i) * 8
|
||||
(this ushr offset).toUByte()
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
return order.normalize(bytes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user