我真正想知道的是迅速进行转换的最快方法是什么
这些是我想迅速进行的对话
答案 0 :(得分:2)
使用本地String
和Int
初始化程序:
extension String {
func convertBase(from: Int, to: Int) -> String? {
return Int(self, radix: from)
.map { String($0, radix: to) }
}
}
let binary = "000010001"
let decadic = binary.convertBase(from: 2, to: 10)
print(decadic)
let hexadecimal = binary.convertBase(from: 2, to: 16)
print(hexadecimal)