![](/img/trans.png)
[英]How to convert hexadecimal string to an array of UInt8 bytes in Swift?
[英]Swift Array<UInt8> convert to string
在以下情况下,我需要帮助。 我需要使用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
当我尝试将字节转换为String时,我总是得到“ NIL”值。
我找到了想要的答案。
let ciphertext = try aes.encrypt(Array("Password123".utf8))
let base64EncodeString = ciphertext.toBase64()
print("ciphertext: \(base64EncodeString!)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.