Hi, eveyone Is where any multiplatform Base64 libr...
# multiplatform
n
Hi, eveyone Is where any multiplatform Base64 library? Or maybe where is a way to convert string to NSString? I need to encode string with base64 in actual class for iOS platform
o
Feel free to create .def file with https://github.com/JetBrains/kotlin-native/blob/master/common/src/hash/cpp/Base64.cpp which encodes/decodes string to base64.
s
Or maybe where is a way to convert string to NSString?
Sure.
str as NSString
.
n
@svyatoslav.scherbina , thanks but for me compiler say “this cast can never success”.
@olonho Thanks
s
@Nikolai just ignore this warning.
n
@svyatoslav.scherbina Ok, thanks I will try
l
@svyatoslav.scherbina about that warning, is there an existing issue we can follow to remove it in that special case? I'd rather not take the habit to ignore warnings.
j
My version of iOS Base64 encoder (I work without padding so I remove it)
Copy code
/** Object to convert base 64 */
actual object Base64 {
    /** Decode [base64] string into bytes array */
    actual fun decode(base64: String): ByteArray {
        // For this library end padding is mandatory so add possible missing padding
        val value = base64.padEnd(
            length = ((base64.length + 3) / 4) * 4,
            padChar = '='
        )

        val data = NSData.create(value, 0) ?: throw ParseException("Invalid Base64 value $base64")
        @Suppress("UNCHECKED_CAST")
        val bytePtr = (data.bytes as CPointer<uint8_tVar>)

        return ByteArray(data.length.toInt()) { index ->
            bytePtr[index].toByte()
        }
    }

    /** Encode [bytes] array into a base64 String */
    actual fun encode(bytes: ByteArray) = memScoped {
        NSData.create(
            bytesNoCopy = bytes.toCValues().getPointer(this),
            length = bytes.size.toULong()
        ).base64EncodedStringWithOptions(0).dropLastWhile {
            // Remove any Base64 padding
            it == '='
        }
    }
}
s
@louiscad feel free to create one. Alternatively one can use
NSString.create(string = str)
.
l
r
@svyatoslav.scherbina Cannot use
NSString.create(string = str)
in K/N 1.4.10
s
Cannot use 
NSString.create(string = str)
 in K/N 1.4.10
According to your screenshot, you don’t use the named argument (
string =
), which is important here.
NSString.create(string = str)
totally works for me.
r
Get it, thanks.