我需要以下场景的帮助。我需要用 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/