I'm working in Swift and I have this string as input: "0x13123ac"
And I want an Int
as output: 0x13123ac
How can I do that?
I'm working in Swift and I have this string as input: "0x13123ac"
And I want an Int
as output: 0x13123ac
How can I do that?
Your question makes no sense. Computers store numbers in binary, not hex. Perhaps what you wanted was to convert a hex string to an integer and convert it back to hex for display purposes:
let input = "0x13123ac"
// Int(_, radix: ) can't deal with the '0x' prefix. NSScanner can handle hex
// with or without the '0x' prefix
let scanner = Scanner(string: input)
var value: UInt64 = 0
if scanner.scanHexInt64(&value) {
print("Decimal: \(value)")
print("Hex: 0x\(String(value, radix: 16))")
}