gpt4 book ai didi

ios - 将 Base64string 转换为字符串时出现问题

转载 作者:行者123 更新时间:2023-11-29 05:39:54 25 4
gpt4 key购买 nike

我正在尝试将 base64 字符串转换为字符串格式,但我总是得到 nil。我的base64字符串是“NWQwMDU2ZjhiZjRjYmI2M2MxZTI0NzQzNjAxMjMxMzAyMDh8NjAxMjMxMzAyMDh8NWQwMDU2Zjhi\nZjRjYmI2M2MxZTI0NzQzfDYwMTIzMTMwMjA4fG5hdmlnYXRp b25UZXN0MDA1fDIwMTktMDYtMjEg\nMDk6MzQ6MDB8MA==\n"

解码后,应该看起来像这样“5d0056f8bf4cbb63c1e2474360123130208|60123130208|5d0056f8bэ͌Ŕ٥ѥQÂs3C�”。这是我的代码事件,我尝试从字符串中删除“=\n”,但没有成功。

 func qrScanningSucceededWithCode(_ str: String?) {
scanTicketView.qrData = QRData(codeString: str)
let charsToRemove: Set<Character> = Set("=\n")
let newNumberCharacters = String(str!.filter { !charsToRemove.contains($0) })
let decodedString = String(data: Data(base64Encoded: newNumberCharacters)!, encoding: .utf8)!
print(decodedString)
}

最佳答案

这有效:

let string = "NWQwMDU2ZjhiZjRjYmI2M2MxZTI0NzQzNjAxMjMxMzAyMDh8NjAxMjMxMzAyMDh8NWQwMDU2Zjhi\nZjRjYmI2M2MxZTI0NzQzfDYwMTIzMTMwMjA4fG5hdmlnYXRpb25UZXN0MDA1fDIwMTktMDYtMjEg\nMDk6MzQ6MDB8MA==\n"

let joined = string.replacingOccurrences(of: "\n", with: "")

if let data = Data(base64Encoded: joined) {
String(data: data, encoding: .utf8)
}

关于ios - 将 Base64string 转换为字符串时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com