在swift中将hex转换为十进制
如何在swift中将hex转换为十进制?
我有字符串“423fdf4dfb1115”,我需要得到这个=> 18647576781459733。
我试试这个:
let result = UInt8(strtoul("423fdf4dfb1115", nil, 16))
但是app崩溃……
我使用这个在线转换: http : //www.binaryhexconverter.com/hex-to-decimal-converter
结果必须是UInt64
( UInt8.max
是255)。
有一个方便的初始化程序,不需要strtoul
。
let string = "423fdf4dfb1115" let result = UInt64(string, radix:16)
let result = UInt64(strtoul(“423fdf4dfb1115”,nil,16))
你需要让Int更好地使用Int()
并且应该说它是hexradix: 16
如下所示。
斯威夫特3
let result = "423fdf4dfb1115" let intResult = Int(result , radix: 16) print(intResult)