简体   繁体   中英

Convert hex to decimal in 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

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)

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

You need to get Int so better to use Int() and should say it's hex radix: 16 Like below.

Swift 3

let result =  "423fdf4dfb1115"
let intResult =  Int(result , radix: 16)
print(intResult)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM