如何在 C# 中将 IPv4 地址转换为整数?
我正在寻找一个将标准 IPv4 地址转换为整数的函数。 如果函数执行相反的操作,则可获得奖励积分。
解决方案应该是 C# 。
I'm looking for a function that will convert a standard IPv4 address into an Integer. Bonus points available for a function that will do the opposite.
Solution should be in C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
32 位无符号整数是 IPv4 地址。 同时,
IPAddress.Address
属性虽然已弃用,但它是一个 Int64,它返回 IPv4 地址的无符号 32 位值(问题是,它采用网络字节顺序,因此您需要交换它)大约)。例如,我的本地 google.com 位于
64.233.187.99
。 这相当于:事实上, http:// /1089059683/ 按预期工作(至少在 Windows 中,使用 IE、Firefox 和 Chrome 进行测试;但在 iPhone 上不起作用)。
这是一个测试程序,用于显示两种转换,包括网络/主机字节交换:
32-bit unsigned integers are IPv4 addresses. Meanwhile, the
IPAddress.Address
property, while deprecated, is an Int64 that returns the unsigned 32-bit value of the IPv4 address (the catch is, it's in network byte order, so you need to swap it around).For example, my local google.com is at
64.233.187.99
. That's equivalent to:And indeed, http://1089059683/ works as expected (at least in Windows, tested with IE, Firefox and Chrome; doesn't work on iPhone though).
Here's a test program to show both conversions, including the network/host byte swapping:
以下是从 IPv4 到正确整数之间相互转换的两种方法:
示例
说明
IP 地址采用网络顺序(大端) ,而
int
在 Windows 上是小端字节序,因此要获得正确的值,必须在小端字节序系统上转换之前反转字节。此外,即使对于
IPv4
,int
也无法保存大于127.255.255.255
的地址,例如广播地址(255.255. 255.255)
,因此请使用uint
。Here's a pair of methods to convert from IPv4 to a correct integer and back:
Example
Explanation
IP addresses are in network order (big-endian), while
int
s are little-endian on Windows, so to get a correct value, you must reverse the bytes before converting on a little-endian system.Also, even for
IPv4
, anint
can't hold addresses bigger than127.255.255.255
, e.g. the broadcast address(255.255.255.255)
, so use auint
.@Barry Kelly 和@Andrew Hare,实际上,我不认为乘法是最明确的方法(尽管是正确的)。
Int32“格式”的 IP 地址可以视为以下结构
因此,要转换 ip 地址 64.233.187.99,您可以这样做:
因此您可以使用 + 将它们相加,或者可以将它们二进制或在一起。 结果是 0x40E9BB63,即 1089059683。(在我看来,以十六进制查看更容易看到字节)
因此您可以将函数编写为:
@Barry Kelly and @Andrew Hare, actually, I don't think multiplying is the most clear way to do this (alltough correct).
An Int32 "formatted" IP address can be seen as the following structure
So to convert the ip address 64.233.187.99 you could do:
so you could add them up using + or you could binairy or them together. Resulting in 0x40E9BB63 which is 1089059683. (In my opinion looking in hex it's much easier to see the bytes)
So you could write the function as:
试试这个:
Try this ones:
当面对具有非常大值的 IP 地址时,我遇到了所描述的解决方案的一些问题。
结果是,byte[0] * 16777216 会溢出并变成负 int 值。
对我来说解决这个问题的是一个简单的类型转换操作。
I have encountered some problems with the described solutions, when facing IP Adresses with a very large value.
The result would be, that the byte[0] * 16777216 thingy would overflow and become a negative int value.
what fixed it for me, is the a simple type casting operation.
由于没有人发布使用 BitConverter 并实际检查字节序的代码,因此如下:
然后返回:
As noone posted the code that uses
BitConverter
and actually checks the endianness, here goes:and back:
戴维·兰德曼函数的逆函数
The reverse of Davy Landman's function
将上述几个答案组装成一个扩展方法,该方法处理机器的字节顺序并处理映射到 IPv6 的 IPv4 地址。
单元测试:
Assembled several of the above answers into an extension method that handles the Endianness of the machine and handles IPv4 addresses that were mapped to IPv6.
Unit tests:
使用正确的小端格式的 UInt32,这里有两个简单的转换函数:
With the UInt32 in the proper little-endian format, here are two simple conversion functions:
我的问题已关闭,我不知道为什么。 这里接受的答案与我需要的不一样。
这给了我正确的 IP 整数值。
My question was closed, I have no idea why . The accepted answer here is not the same as what I need.
This gives me the correct integer value for an IP..
上面的例子就是我要走的路。您可能需要做的唯一一件事就是转换为 UInt32 以用于显示目的,或字符串目的,包括将其用作字符串形式的长地址。
这是使用 IPAddress.Parse(String) 函数时所需要的。 叹。
The above example would be the way I go.. Only thing you might have to do is convert to a UInt32 for display purposes, or string purposes including using it as a long address in string form.
Which is what is needed when using the IPAddress.Parse(String) function. Sigh.
如果您对该函数感兴趣,那么这里的答案不仅仅是它是如何完成的:
first
到fourth
是 IPv4 地址的段。If you were interested in the function not just the answer here is how it is done:
with
first
throughfourth
being the segments of the IPv4 address.将 IP 号的所有部分乘以 256 的幂(256x256x256、256x256、256 和 1)。例如:
IPv4 地址:127.0.0.1
32位数字:
= (127x256^3) + (0x256^2) + (0x256^1) + 1
=2130706433
Multiply all the parts of the IP number by powers of 256 (256x256x256, 256x256, 256 and 1. For example:
IPv4 address : 127.0.0.1
32 bit number:
= (127x256^3) + (0x256^2) + (0x256^1) + 1
= 2130706433
这是我今天找到的一个解决方案(应该先用 google 搜索一下!):
我正在使用 LINQ、lambda 和一些泛型扩展,因此虽然它产生相同的结果,但它使用了一些新的语言功能,您可以只需三行代码即可完成。
如果您有兴趣,我的博客上有解释。
干杯,
-jc
here's a solution that I worked out today (should've googled first!):
i'm using LINQ, lambda and some of the extensions on generics, so while it produces the same result it uses some of the new language features and you can do it in three lines of code.
i have the explanation on my blog if you're interested.
cheers,
-jc
我认为这是错误的:“65536”==> 0.0.255.255"
应该是:“65535”==> 0.0.255.255”或“65536”==> 0.1.0.0”
I think this is wrong: "65536" ==> 0.0.255.255"
Should be: "65535" ==> 0.0.255.255" or "65536" ==> 0.1.0.0"
@Davy Ladman,您的移位解决方案是正确的,但仅适用于以小于或等于 99 的数字开头的 ip,事实上第一个八位字节必须强制转换为 long。
无论如何,用 long 类型转换回来是相当困难的,因为存储 64 位(不是 Ip 的 32 位)并用零填充 4 个字节
享受吧!
马西莫
@Davy Ladman your solution with shift are corrent but only for ip starting with number less or equal 99, infact first octect must be cast up to long.
Anyway convert back with long type is quite difficult because store 64 bit (not 32 for Ip) and fill 4 bytes with zeroes
Enjoy!
Massimo
假设您有一个字符串格式的 IP 地址(例如 254.254.254.254)
Assuming you have an IP Address in string format (eg. 254.254.254.254)
我用这个:
I use this:
看一下 .Net 的 IPAddress.Parse 中的一些疯狂解析示例:
(MSDN)
“65536”== > 0.0.255.255
“20.2”==> 20.0.0.2
“20.65535”==> 20.0.255.255
“128.1.2”==> 128.1.0.2
Take a look at some of the crazy parsing examples in .Net's IPAddress.Parse:
(MSDN)
"65536" ==> 0.0.255.255
"20.2" ==> 20.0.0.2
"20.65535" ==> 20.0.255.255
"128.1.2" ==> 128.1.0.2
我注意到 System.Net.IPAddress 有 Address 属性 (System.Int64) 和构造函数,它们也接受 Int64 数据类型。 因此,您可以使用它来将 IP 地址与数字(虽然不是 Int32,而是 Int64)格式相互转换。
I noticed that System.Net.IPAddress have Address property (System.Int64) and constructor, which also accept Int64 data type. So you can use this to convert IP address to/from numeric (although not Int32, but Int64) format.