Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex to Decimal - Swift

Tags:

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

like image 704
JoeBayLD Avatar asked Feb 20 '15 03:02

JoeBayLD


2 Answers

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

let string = "ff"
if let value = UInt8(string, radix: 16) {
    print(value)
}
like image 156
Rob Avatar answered Oct 06 '22 00:10

Rob


Try this code, It's work for me.

// Hex to decimal

let h2 = "ff"
let d4 = Int(h2, radix: 16)!
print(d4) 

Hope this is help for some one

like image 24
Jaywant Khedkar Avatar answered Oct 05 '22 23:10

Jaywant Khedkar