如何比较两个 IP 地址,看看哪一个更大?

发布于 2024-09-18 16:39:21 字数 151 浏览 4 评论 0原文

例如,下面有两组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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假扮的天使 2024-09-25 16:39:21

如果将它们转换为 32 位整数,则较大的整数将为较大的整数。换句话说:

10.192.3.177 -> 0x0ac003b1 (180,356,017) (bigger)
10.192.2.204 -> 0x0ac002cc (180,355,788)

10.192.3.205 -> 0x0ac003cd (180,356,045) (bigger)
10.192.2.204 -> 0x0ac002cc (180,355,788)

我很难想象一个重要的用例,但如果我必须签入程序,这就是我会采取的方法。

If you convert them to a 32-bit integer, the greater one will be the greater integer. In other words:

10.192.3.177 -> 0x0ac003b1 (180,356,017) (bigger)
10.192.2.204 -> 0x0ac002cc (180,355,788)

10.192.3.205 -> 0x0ac003cd (180,356,045) (bigger)
10.192.2.204 -> 0x0ac002cc (180,355,788)

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.

怪异←思 2024-09-25 16:39:21

您可能应该澄清“更大”的含义。

但每个 IP 地址的数值 (uint32) 可以通过以下方式计算:

d + 256 * c + 65536 * b + 16777216 * a

其中 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:

d + 256 * c + 65536 * b + 16777216 * a

where a, b, c, and d are the base 10 values in an IPv4 formatted:
a.b.c.d

仲春光 2024-09-25 16:39:21

如果您正在寻找一种对 IP 列表进行排序的方法,您还可以将 IP 存储在字符串中,每个八位字节前面添加 0 到 3 位数字。然后文本排序就可以正常工作了。

例子:

010.192.002.204
010.192.003.177 

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:

010.192.002.204
010.192.003.177 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文