這是代碼一直讓我發瘋,它是如此簡單但無法讓它作業。我遵循了論壇的幾個示例和幫助,但 XOR 不起作用。問題是當我從字串陣列中提取字符并將其轉換為 ASCII 值時,它是 Uint8 而不是 Int。所以 XOR 不起作用,如何將 Uint8 轉換為 int?
// Convert the data into the string
for n in 0...7
{
print("Inside the password generator for loop /(n)")
let a:Character = SerialNumber_hex_array[n]
var a_int = a.asciiValue
let b:Character = salt_arrary[n]
let b_int = b.asciiValue
// This does not work as the a_int & b_int are not int !!!
// How to convert the Uint8 to int?
let xor = (a_int ^ b_int)
// This code works
var a1 = 12
var b1 = 25
var result = a1 ^ b1
print(result) // 21
}
uj5u.com熱心網友回復:
要將您的轉換UInt8?為Int,請使用可用的Int初始化程式:
let a_int = Int(a.asciiValue!)
let b_int = Int(b.asciiValue!)
let xor = (a_int ^ b_int) // compiles
這種直接的方法需要強制展開,但我假設十六進制陣列如下所示,并且為了安全起見,您的字符是硬編碼的。if-else如果不是,請使用or安全地解開這些無符號整數guard。
let SerialNumber_hex_array: [Character] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439962.html
