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