gpt4 book ai didi

iOS - 检查掩码 ip 是否有效(在 ip 范围内)

转载 作者:行者123 更新时间:2023-11-28 14:14:26 25 4
gpt4 key购买 nike

所以我想弄清楚如何根据 ip 验证掩码 ip(在范围内?)。我似乎无法像在 java 中那样快速找到工具:

How can I detect if an IP is in a network?

最佳答案

let ip = "192.168.2.0"
let net = "192.168.1.0"
let pref = 24

func convertIpToInt(_ ipAddress: String) -> Int? {
var result = 0.0
let ipAddressArray = ipAddress.components(separatedBy: ".").compactMap {
Double($0) }
guard ipAddressArray.count == 4 else { return nil }
for (index, element) in ipAddressArray.enumerated() {
result += element * pow(256, Double(3 - index))
}

return Int(result) > 0 ? Int(result) : nil
}

let ipInt = convertIpToInt(ip)
let netInt = convertIpToInt(net)
let brkstInt = netInt! + Int(pow(2, Double(32-pref))) - 1


print(ipInt! >= netInt! && ipInt! <= brkstInt) // false

关于iOS - 检查掩码 ip 是否有效(在 ip 范围内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52259279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com