Most likely a stupid question :smile: I have a `N...
# kotlin-native
c
Most likely a stupid question 😄 I have a
NSData
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)
            }

            ....
   }
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
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
Ahh. got to know! thanks!
Working with C pointers in Kotlin is not my favourite though 😄