将 IP 地址转换为数字:

发布于 2024-09-03 15:48:54 字数 553 浏览 4 评论 0原文

问题: 当我将IP地址192.168.115.67转换为数字时,是这样完成的:
192*2563 + 168*2562+115*2561+67*2560 = 3232265027

或者像这样:
192*2560 + 168*2561+115*2562+67*2563 = 1131653312

我在网上找到了这两个变体,坦率地说,只要我使用相同的转换过程变体进行所有内部 IP 范围比较,这并不重要。 但我想从 IPv4 地址计算 IP V6,而且似乎这两种变体都在网络上...... 导致不同的 IPv6 地址,并且只有一个是正确的......

我使用 1131653312 变体,因为 1131653312 是我看到 .NET 给我的变体,但 3232265027 是我在 C++ 中使用的变体,这也是我在网上找到的用于 IPv4 到 IPv6 转换的变体,并且在我看到 .NET 使用变体 1131653312 之前我就使用过它......

Question:
When I convert the IP address 192.168.115.67 to a number, is it done like this:
192*2563 + 168*2562+115*2561+67*2560 = 3232265027

or like this:
192*2560 + 168*2561+115*2562+67*2563 = 1131653312

I find both variants online, and frankly it doesn't matter as long as I do all the internal IP-range comparison using the same conversion process variant.
But I want to calculate the IP V6 from the IPv4 address, and it seems both variants are on the web...
resulting in different IPv6 addresses, and only one can be correct...

I use the 1131653312 variant, as 1131653312 is the variant I saw .NET giving me, but 3232265027 is the variant I used when I did it in C++, and that is also the variant I find on the web for IPv4 to IPv6 conversion, and which I used before I saw that .NET uses variant 1131653312 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

沫雨熙 2024-09-10 15:48:54

这绝对是第一个。您可以 ping 并查看 ping 实用程序如何将其转换为 abcd 表示法。如果您要进行此转换,我建议使用表达式: (a << 24) | (b<<16)| (c << 8) | d

It is definitely first one. You can ping and see how ping utility convert it to a.b.c.d notation. If you going to do this conversion I recommend expression: (a << 24) | (b << 16) | (c << 8) | d

欢你一世 2024-09-10 15:48:54

生成 3232265027 的方法应该是正确的,因为第一个数字存储在 IPv6 的 IPv4 兼容性部分的最高位中:

0000:0000:0000:0000:0000:ffff:AABB :CCDD

其中

AA = 192
BB = 168
CC = 0
DD = 1

https://wikipedia.org/wiki/IPv6

此外,IP 地址中不能包含 256。每个值为 0 - 255。

The method resulting in 3232265027 should be correct, as the first number is stored in the highest bits of the IPv4 compatibility part of IPv6:

0000:0000:0000:0000:0000:ffff:AABB:CCDD

where

AA = 192
BB = 168
CC = 0
DD = 1

etc.

https://wikipedia.org/wiki/IPv6

Also, you can't have a 256 in an IP address. The values are 0 - 255 each.

剪不断理还乱 2024-09-10 15:48:54

数字以大端顺序通过线路发送,这与它们的写入方式相同(192 是最高有效字节)。因此第一个数字是“正确”的。另一个变体是如何在小端架构上解释相同的字节,从而产生 .Net 行为。

说了这么多,我真的不确定问题是什么,所以我只能希望这个阐述能构成某种答案。

Numbers are sent in big-endian order over the wire, which is the same as the way they are written (192 is the most significant byte). Thus the first number is the "correct" one. The other variant is how the same bytes will be interpreted on a little-endian architecture, hence the .Net behaviour.

Having said all that, I'm really not sure what the question is, so I can only hope that this exposition constitutes an answer of sorts.

清秋悲枫 2024-09-10 15:48:54

差异与字节序有关。您将其存储在整数中,但不同的处理器对其整数有不同的字节顺序(大端或小端在前)。然而,该数字必须通过线路输出,因此 IP 有一个特定的顺序,它需要传输字节。该顺序对于 bigendian 和 Littleendian 机器来说看起来不同。

The difference has to do with endianness. You are storing it in an integer, but different processors have different byte orderings for their integers (big or little end first). However, that number has to go out over the line, so IP has a specific order it needs the bytes transferred in. That order looks different to bigendian and littleendian machines.

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