I am working on an iOS app, which uses multiplatform Kotlin. I need to encode/decode a string into base64. I am able to encode a normal string with below code, but I am not able to decode a base 64 string into normal string. Below is my code.
fun encodeToBase64() {
var st: NSString = "normalString"
var data: NSData? = st.dataUsingEncoding(encoding =
NSUTF8StringEncoding)
if (data != null) {
var str = data.base64EncodedStringWithOptions(options = 0)
println("base 64 string == $str")
}
}
Thanks
For anyone still looking for base64 encoding and decoding and/or hashing in multiplatform projects. The Okio library has multiplatform support and can be used as a kotlin native solution. (https://square.github.io/okio/multiplatform/)
fun shar256(input: String): String? = input.encodeUtf8().sha256()
fun base64Encoded(input: String): String? = input.encodeUtf8().base64()
fun base64Decoded(input: String): String? = input.decodeBase64()?.utf8()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With