简体   繁体   English

多平台 Kotlin 中 iOS 的 Base 64 编码解码

[英]Base 64 Encoding Decoding for iOS in Multiplatform Kotlin

I am working on an iOS app, which uses multiplatform Kotlin.我正在开发一个使用多平台 Kotlin 的 iOS 应用程序。 I need to encode/decode a string into base64.我需要将字符串编码/解码为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.我可以使用以下代码对普通字符串进行编码,但无法将 base 64 字符串解码为普通字符串。 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.对于仍在多平台项目中寻找 base64 编码和解码和/或散列的任何人。 The Okio library has multiplatform support and can be used as a kotlin native solution. Okio 库具有多平台支持,可用作 kotlin 原生解决方案。 ( https://square.github.io/okio/multiplatform/ ) ( 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM