-
-
Save indramahkota/a7e02870e30bf8b7c1837a984cb4fc13 to your computer and use it in GitHub Desktop.
Revisions
-
noahsark769 created this gist
Oct 16, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ 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 = this@toData.size.toULong()) } public fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt()).apply { usePinned { memcpy(it.addressOf(0), this@toByteArray.bytes, this@toByteArray.length) } }