Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert hex to decimal in swift

Tags:

xcode

ios

hex

swift

how to convert hex to decimal in swift?

I have string "423fdf4dfb1115" and I need get this => 18647576781459733.

I try this:

let result = UInt8(strtoul("423fdf4dfb1115", nil, 16)) 

but app crash...

I use this online convert: http://www.binaryhexconverter.com/hex-to-decimal-converter

like image 436
Vykintas Avatar asked Dec 14 '22 05:12

Vykintas


2 Answers

The result must be UInt64 (UInt8.max is 255).

There is a convenience initializer, strtoul is not needed.

let string = "423fdf4dfb1115"
let result = UInt64(string, radix:16)
like image 188
vadian Avatar answered Dec 16 '22 18:12

vadian


let result = UInt64(strtoul("423fdf4dfb1115", nil, 16))

like image 45
haitham Avatar answered Dec 16 '22 18:12

haitham