and need to get the first two and the last two bytes out of it…any clue how I can do that? Below gives me the first two, but I don’t see any way to offset the
bytes
pointer by
length - 2
to get the last two
Copy code
private fun NSData.isCompleteJpeg(): Boolean {
val byteArray = ByteArray(2)
byteArray.usePinned {
memcpy(it.addressOf(0), bytes, 2)
}
....
}
Christian Würthenr
10/25/2022, 11:15 AM
Haha 😄 Never mind. Stumbled over the solution myself. The trick is to use
reinterpret
to get a
UByte
pointer and the you can use the
[]
to get a certain index
Copy code
val x = bytes!!.reinterpret<UByteVar>()
val s1 = x[0]
val s2 = x[1]
val e1 = x[length.toInt() - 2]
val e2 = x[length.toInt() - 1]
n
natario1
10/25/2022, 11:20 PM
Pointers also have a plus() function that you can use to move the pointer by a certain number of elements. You'd still need to reinterpret first though
c
Christian Würthenr
10/26/2022, 6:38 AM
Ahh. got to know! thanks!
Christian Würthenr
10/26/2022, 6:38 AM
Working with C pointers in Kotlin is not my favourite though 😄