我正在读取一个十六进制值并将其转换为二进制数,但是,它不会打印出前导零。我知道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。 –