All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iosMain.dev.bluefalcon.NSData.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package dev.bluefalcon

import kotlinx.cinterop.*
import platform.Foundation.*
import platform.posix.memcpy

fun NSData.string(): String? {
    return NSString.create(this, NSUTF8StringEncoding) as String?
}


@OptIn(ExperimentalForeignApi::class)
fun NSData.toByteArray(): ByteArray {
    val data = this
    val d = memScoped { data }
    return ByteArray(d.length.toInt()).apply {
        usePinned {
            memcpy(it.addressOf(0), d.bytes, d.length)
        }
    }
}

@OptIn(ExperimentalForeignApi::class)
fun ByteArray.toData(): NSData = memScoped {
    NSData.create(bytes = allocArrayOf(this@toData),
        length = [email protected]())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy