Created
October 16, 2020 19:10
-
-
Save noahsark769/61cfb7a8b7231e2069a9dab94cf74a62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. Its really helpful.