iOS Swift 使用 NO_WRAP 选项将 HEX 字符串转换为 Base64

标签 ios swift

Android 的 Base 64 NO_WRAP 选项在 swift 的 Base 64 中是否等效?

在安卓中: hash = Base64.encodeToString(message.getBytes()), Base64.NO_WRAP);

参见下面的 NO_WRAP 选项: https://developer.android.com/reference/android/util/Base64.html

在 iOS 中,使用 NO_WRAP 选项将 HEX 字符串转换为 Base 64 的 swift 等效项是什么?

最佳答案

在文档中:

因此在编码时完全不指定任何选项以避免添加任何行结束字符,并在解码时指定 ignoreUnknownCharacters 以忽略行结束字符。

第 1 步:Hex String to Data is a separate StackOverflow question (但是你应该避免使用十六进制字符串,这是对字节的巨大浪费!)

第二步:数据转base64数据或base64字符串(二选一)

// By default, no line endings are inserted: https://developer.apple.com/reference/foundation/nsdata/1412739-base64encodeddata
let encodedAsData = data.base64EncodedData()

// By default, no line endings are inserted: https://developer.apple.com/reference/foundation/nsdata/1413546-base64encodedstring
let encodedAsString = data.base64EncodedString()

关于iOS Swift 使用 NO_WRAP 选项将 HEX 字符串转换为 Base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754462/

相关文章:

ios - 通过蓝牙发送联系人

ios - 无法转换 swift 类型的值

ios - 无法在 tableviewcontroller 上获得半透明的导航栏。适用于 View Controller 和 TableView

ios - Google places api 应该只返回地址

javascript - 如何在同一屏幕内更改 View (React Native)?

ios - 我如何使用 Codable 来解析这个 JSON?

ios - 编辑表格 View 时编辑电池导出

ios - 从 iOS 原生 WebView 使用 Facebook 进行服务器端登录

ios - 如何保存和加载具有自定义名称的 .txt 文件?

ios - CollectionView 中的刷新数据不起作用 Swift