我正在讀取一個十六進制值並將其轉換爲二進制數,但是,它不會打印出前導零。我知道swift沒有像C這樣的內置功能。我想知道是否有方法打印出任何前導零,當我知道最大的二進制數將是16個字符。我有一些代碼爲我運行,取十六進制數字,將其轉換成十進制數字,然後轉換爲二進制數字。Swift中的前導二進制零字
@IBAction func HextoBinary(_ sender: Any)
{
//Removes all white space and recognizes only text
let origHex = textView.text.trimmingCharacters(in: .whitespacesAndNewlines)
if let hexNumb_int = Int(origHex, radix:16)
{
let decNumb_str = String(hexNumb_int, radix:2)
textView.text = decNumb_str
}
}
任何幫助,非常感謝。
鏈接到的「重複」具有代碼夫特1,2,和3。 –