czuckie
10/08/2022, 5:10 PMList<Byte>
) into a list of Shorts? (List<Short>
) such that [ 0x10, 0x01 ] would become [ 0x1001 ]czuckie
10/08/2022, 5:18 PMprivate fun List<Byte>.toShorts(): List<Short> {
val result = mutableListOf<Short>()
for (i in 0 .. size - 2 step 2) {
result.add((this[i].toInt().shl(8) + this[i+1]).toShort())
}
return result
}
Is what I've got, which is I guess understandable, but I wonder if there is a more kotlin'y way to iterate over pairs of list items to them map them into something elsebezrukov
10/08/2022, 5:18 PMephemient
10/09/2022, 4:12 AMfor (i in indices step 2) {
or change indexing to something which also pre-sizes the output
List(size / 2) { i ->
(this[2 * i].toUInt() shl 8 or this[2 * i + 1].toUInt()).toShort()
}
czuckie
10/09/2022, 2:45 PM