【发布时间】:2016-12-06 03:50:00
【问题描述】:
我正在接受一个十六进制值并将其转换为二进制数,但是它没有打印出前导零。我知道 swift 没有像 C 那样的内置功能。我想知道当我知道二进制数的最大值是 16 个字符时,是否有办法打印出任何前导零。我有一些代码可以通过获取十六进制数,将其转换为十进制数,然后转换为二进制数来运行。
@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
}
}
非常感谢任何帮助。
【问题讨论】:
-
链接到的“重复”具有 Swift 1、2 和 3 的代码。