ios - Swift Array<UInt8> 转换为字符串

标签 ios swift

我需要以下场景的帮助。我需要用 Swift 语言将字节转换为字符串。这是我的代码。

let ciphertext = try aes.encrypt(Array("Password123".utf8))
print("ciphertext: \(ciphertext)")
// output ciphertext: [148, 177, 49, 193, 100, 184, 232, 51, 26, 170, 146, 114, 77, 244, 29, 172]

let nsdata = NSData(bytes: ciphertext as [UInt8], length: ciphertext.count)
print("nsdata: \(nsdata)")
// output nsdata: <94b131c1 64b8e833 1aaa9272 4df41dac>

let str = String(data: nsdata as Data, encoding: String.Encoding.utf8)
print("cipherStr: \(str)")
// output cipherStr: nil

当我尝试将字节转换为字符串时,我总是得到“NIL”值。

最佳答案

我找到了我想要的答案。

let ciphertext = try aes.encrypt(Array("Password123".utf8))
let base64EncodeString = ciphertext.toBase64()            
print("ciphertext: \(base64EncodeString!)")

关于ios - Swift Array<UInt8> 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49442191/

相关文章:

ios - 导入 mymodel.mlmodel 时出现 CoreML : source file is not valid , 错误

ios - 获取当前设备,也在模拟器中

ios - 如何测试依赖于 UIUserInterfaceIdiom (ios) 的代码

ios - UIButtonTypeInfoDark Touch Area

ios - CTCallCenter 崩溃

swift - 以编程方式更改 UIImage 位置

ios - 从 UICollectionViewCell 中删除单元格和相应的图像?

ios - 使用 CoreData 和依赖注入(inject) - 线程 1 : Fatal error: init(coder:) has not been implemented

ios - 什么是 objc_AssociationPolicy?它是如何工作的?

arrays - 修复 fatal error : Index out of range in arrays