转换十六进制字串的Swift int构造函数

时间:2023-01-05 18:47:00

All these calls return nil for me:

所有这些调用都为我返回nil:

Int("ff000000", radix: 16)
Int("#ff000000", radix: 16)
Int("0xff000000", radix: 16)

I must be doing something wrong.

我一定是做错了什么。

1 个解决方案

#1


2  

Use this code, this is working properly.

使用这段代码,这段代码运行正常。

let value1 = Int64("aabb00ddff",radix: 16)

转换十六进制字串的Swift int构造函数

The instead of using Int you should use Int64, because when you convert Hex to Int resulted value will be bigger than expected.

与使用Int不同,您应该使用Int64,因为当您将十六进制转换为Int结果值时,结果值将大于预期值。

Int will also work.

Int也会工作。

#1


2  

Use this code, this is working properly.

使用这段代码,这段代码运行正常。

let value1 = Int64("aabb00ddff",radix: 16)

转换十六进制字串的Swift int构造函数

The instead of using Int you should use Int64, because when you convert Hex to Int resulted value will be bigger than expected.

与使用Int不同,您应该使用Int64,因为当您将十六进制转换为Int结果值时,结果值将大于预期值。

Int will also work.

Int也会工作。