如何比较两个 IP 地址,看看哪一个更大?
例如,下面有两组IP地址,现在,您如何识别每组中较大的IP?
A 组:
10.192.3.177 和 10.192.2.204
B 组:
10.192.3.205 和 10.192.2.204
谢谢!
For example, following are two sets of IP addresses, now, how would you identify the greater IP for each set?
SET A:
10.192.3.177 and 10.192.2.204
SET B:
10.192.3.205 and 10.192.2.204
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将它们转换为 32 位整数,则较大的整数将为较大的整数。换句话说:
我很难想象一个重要的用例,但如果我必须签入程序,这就是我会采取的方法。
If you convert them to a 32-bit integer, the greater one will be the greater integer. In other words:
I'm having a hard time imagining a use case where it would matter but that's the approach I would take if I had to check in a program.
您可能应该澄清“更大”的含义。
但每个 IP 地址的数值 (uint32) 可以通过以下方式计算:
其中 a、b、c 和 d 是 IPv4 格式的以 10 为基数的值:
abcd
You should probably clarify what you mean by "greater".
But the numerical (uint32) value of each IP address can be calculated with:
where a, b, c, and d are the base 10 values in an IPv4 formatted:
a.b.c.d
如果您正在寻找一种对 IP 列表进行排序的方法,您还可以将 IP 存储在字符串中,每个八位字节前面添加 0 到 3 位数字。然后文本排序就可以正常工作了。
例子:
If you're after a way to sort a list of IP's you can also store the IP in a string with each octet prepended by 0 to 3 digits. Then a text sort works fine.
Example: