Hi,
I observed a strange behaviour with the following code:
UInt32(Float("0x7f000001") ?? 0)
// will return 2130706432
atof("0x7f000001")
// will return 2130706433
Why do I have two different results? Bug?
Actually 0x7f000001 is the Hex representation of the IP address 127.0.0.1
So the correct result should be 2130706433
UInt32("2130706433")
// will return 2130706433
Thanks
Why do I have two different results? Bug?
Not a bug. `Float` has only 24-siginificant bits, so cannot represent 32-bit integer value pesicely.
`atof` returns `double`, not `float`.
If you want to get the correct result from a hex represntation, you need to cut "0x" and use `init(_:radix:)`.
let hex = "0x7f000001"
let value = UInt32(hex.dropFirst(2), radix: 16) ?? 0
print(value) //->2130706433