Skip to content

Instantly share code, notes, and snippets.

@noahsark769
Created October 16, 2020 19:10
Show Gist options
  • Save noahsark769/61cfb7a8b7231e2069a9dab94cf74a62 to your computer and use it in GitHub Desktop.
Save noahsark769/61cfb7a8b7231e2069a9dab94cf74a62 to your computer and use it in GitHub Desktop.
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.allocArrayOf
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import platform.Foundation.NSData
import platform.Foundation.create
import platform.posix.memcpy
public fun ByteArray.toData(): NSData = memScoped {
NSData.create(bytes = allocArrayOf(this@toData),
length = [email protected]())
}
public fun NSData.toByteArray(): ByteArray = ByteArray([email protected]()).apply {
usePinned {
memcpy(it.addressOf(0), [email protected], [email protected])
}
}
@ankushkushwaha
Copy link

Thanks for this. Its really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment