What i really want to know is what is the quickest way of converting this in swift
- Binary to Hexadecimal
- Hexadecimal to Binary
- Binary to Decimal
- Decimal to Binary
These are the conversation i am trying to do in swift
What i really want to know is what is the quickest way of converting this in swift
These are the conversation i am trying to do in swift
Using native String
and Int
initializers:
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)