简体   繁体   English

在swift中将十六进制转换为十进制

[英]Convert hex to decimal in swift

how to convert hex to decimal in swift? 如何在swift中将十六进制转换为十进制?

I have string "423fdf4dfb1115" and I need get this => 18647576781459733. 我有字符串“423fdf4dfb1115”,我需要得到这个=> 18647576781459733。

I try this: 我试试这个:

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

but app crash... 但是app崩溃......

I use this online convert: http://www.binaryhexconverter.com/hex-to-decimal-converter 我使用这个在线转换: http//www.binaryhexconverter.com/hex-to-decimal-converter

The result must be UInt64 ( UInt8.max is 255). 结果必须是UInt64UInt8.max是255)。

There is a convenience initializer, strtoul is not needed. 有一个方便的初始化程序,不需要strtoul

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. 你需要让Int更好地使用Int()并且应该说它是十六进制radix: 16如下所示。

Swift 3 斯威夫特3

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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