7

我正在编写使用 Windows IP Helper API 的 C# 代码。我试图调用的函数之一是“ GetBestInterface ”,它采用 IP 的“uint”表示。我需要的是解析 IP 的文本表示以创建“uint”表示。

我通过 Google 找到了一些示例,例如this onethis one,但我很确定应该有一种标准方法来使用 .NET 实现这一目标。唯一的问题是,我找不到这种标准方式。IPAddress.Parse 似乎是在正确的方向,但它没有提供任何获取“uint”表示的方式......

还有一种使用 IP Helper 的方法,使用ParseNetworkString,但同样,我宁愿使用 .NET - 我相信我对 pInvoke 的依赖越少越好。

那么,任何人都知道在 .NET 中执行此操作的标准方法吗?

4

9 回答 9

18

不应该是:

var ipAddress = IPAddress.Parse("some.ip.address");
var ipBytes = ipAddress.GetAddressBytes();
var ip = (uint)ipBytes [0] << 24;
ip += (uint)ipBytes [1] << 16;
ip += (uint)ipBytes [2] <<8;
ip += (uint)ipBytes [3];

?

于 2009-02-16T13:34:14.967 回答
12

MSDNIPAddress.Address 属性(返回 IP 地址的数字表示)已过时,您应该使用GetAddressBytes方法。

您可以使用以下代码将 IP 地址转换为数值:

var ipAddress = IPAddress.Parse("some.ip.address");
var ipBytes = ipAddress.GetAddressBytes();
var ip = (uint)ipBytes [3] << 24;
ip += (uint)ipBytes [2] << 16;
ip += (uint)ipBytes [1] <<8;
ip += (uint)ipBytes [0];

编辑:
正如其他评论者所注意到的,上述代码仅适用于 IPv4 地址。IPv6 地址的长度为 128 位,因此无法按照问题作者的要求将其转换为“uint”。

于 2008-08-31T12:55:45.380 回答
12
var ipuint32 = BitConverter.ToUInt32(IPAddress.Parse("some.ip.address.ipv4").GetAddressBytes(), 0);`

此解决方案比手动位移更容易阅读。

请参阅如何在 C# 中将 IPv4 地址转换为整数?

于 2008-08-31T23:01:31.820 回答
5

您还应该记住IPv4IPv6的长度不同。

于 2008-08-31T14:13:11.370 回答
1

不鼓励使用字节算术,因为它依赖于所有 IP 都是 4 字节的。

于 2008-08-31T12:59:35.993 回答
1
System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.1.1");

byte[] bytes = ipAddress.GetAddressBytes();
for (int i = 0; i < bytes.Length ; i++)
       Console.WriteLine(bytes[i]);

输出将是 192 168 1 1

于 2010-10-10T10:59:33.707 回答
1

完整的解决方案:

public static uint IpStringToUint(string ipString)
{
    var ipAddress = IPAddress.Parse(ipString);
    var ipBytes = ipAddress.GetAddressBytes();
    var ip = (uint)ipBytes [0] << 24;
    ip += (uint)ipBytes [1] << 16;
    ip += (uint)ipBytes [2] <<8;
    ip += (uint)ipBytes [3];
    return ip;
}

public static string IpUintToString(uint ipUint)
{
    var ipBytes = BitConverter.GetBytes(ipUint);
    var ipBytesRevert = new byte[4];
    ipBytesRevert[0] = ipBytes[3];
    ipBytesRevert[1] = ipBytes[2];
    ipBytesRevert[2] = ipBytes[1];
    ipBytesRevert[3] = ipBytes[0];
    return new IPAddress(ipBytesRevert).ToString();
}

反转字节顺序:

public static uint IpStringToUint(string ipString)
{
    return BitConverter.ToUInt32(IPAddress.Parse(ipString).GetAddressBytes(), 0);
}

public static string IpUintToString(uint ipUint)
{
    return new IPAddress(BitConverter.GetBytes(ipUint)).ToString();
}

你可以在这里测试:

https://www.browserling.com/tools/dec-to-ip

http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx

http://www.silisoftware.com/tools/ipconverter.php

于 2018-05-05T10:50:59.410 回答
1

遵守字节顺序的正确解决方案:

var ipBytes = ip.GetAddressBytes();
ulong ip = 0;
if (BitConverter.IsLittleEndian)
{
    ip = (uint) ipBytes[0] << 24;
    ip += (uint) ipBytes[1] << 16;
    ip += (uint) ipBytes[2] << 8;
    ip += (uint) ipBytes[3];
}
else
{
    ip = (uint)ipBytes [3] << 24;
    ip += (uint)ipBytes [2] << 16;
    ip += (uint)ipBytes [1] <<8;
    ip += (uint)ipBytes [0];
}
于 2019-09-17T19:06:46.383 回答
0

我从来没有为这个问题找到一个干净的解决方案(即:.NET Framework 中的类/方法)。我想它只是不可用,除了您提供的解决方案/示例或 Aku 的示例。:(

于 2008-08-31T12:56:19.277 回答