Added toByteArray()

This commit is contained in:
dankito 2020-07-23 13:18:13 +02:00
parent 117bb2e5e0
commit 85572d7fb2
1 changed files with 7 additions and 3 deletions

View File

@ -1,6 +1,4 @@
import kotlinx.cinterop.addressOf import kotlinx.cinterop.*
import kotlinx.cinterop.convert
import kotlinx.cinterop.usePinned
import platform.Foundation.* import platform.Foundation.*
@ -14,3 +12,9 @@ fun ByteArray.toNSData(): NSData = NSMutableData().apply {
appendBytes(it.addressOf(0), size.convert()) appendBytes(it.addressOf(0), size.convert())
} }
} }
fun NSData.toByteArray(): ByteArray {
val data: CPointer<ByteVar> = bytes!!.reinterpret()
return ByteArray(length.toInt()) { index -> data[index] }
}