简体   繁体   English

迅捷数组 <UInt8> 转换为字符串

[英]Swift Array<UInt8> convert to string

I need help for the following scenario. 在以下情况下,我需要帮助。 I need to convert bytes to string in Swift language. 我需要使用Swift语言将字节转换为字符串。 Here is my code. 这是我的代码。

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

I always got "NIL" value when I try to convert bytes to String. 当我尝试将字节转换为String时,我总是得到“ NIL”值。

I found the answer I want. 我找到了想要的答案。

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

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

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