JoeBayLD JoeBayLD - 1 month ago 15
Swift Question

Hex to Decimal - Swift

Sorry if this seems really simple, I just can't find it anywhere online.

I have a UInt8 in hex, I need to get it to a decimal. How do I achieve this in swift?

For example:

"ff"

Thanks

Rob Rob
Answer

If you have a string representation, "ff", you can use UInt8(_:radix:):

let string = "ff"
if let value = UInt8(string, radix: 16) {
    print(value)
}
Comments