C# で IP アドレス文字列を uint 値に解析するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/36831

質問

Windows IP ヘルパー API を使用する C# コードを作成しています。私が呼び出そうとしている関数の 1 つは、「GetBestInterface" これは IP の 'uint' 表現を受け取ります。必要なのは、IP のテキスト表現を解析して「uint」表現を作成することです。

Google でいくつかの例を見つけました。 これです または これです, しかし、.NET でこれを実現する標準的な方法があるはずだと私は確信しています。唯一の問題は、この標準的な方法が見つからないことです。IPAddress.Parse は正しい方向にあるようですが、「uint」表現を取得する方法を提供していません...

IP ヘルパーを使用してこれを行う方法もあります。 ネットワーク文字列の解析, 、しかし、繰り返しになりますが、私は .NET を使用したいと思っています。pInvoke への依存は少ないほど良いと信じています。

それで、.NET でこれを行う標準的な方法を知っている人はいますか?

役に立ちましたか?

解決

MSDN 言う IPAddress.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」に変換することは不可能です。

他のヒント

そうあるべきではないでしょうか:

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];

?

var ipuint32 = BitConverter.ToUInt32(IPAddress.Parse("some.ip.address.ipv4").GetAddressBytes(), 0);`

このソリューションは、手動でビットをシフトするよりも読みやすいです。

見る C# で IPv4 アドレスを整数に変換するにはどうすればよいですか?

また、覚えておくべきことは、 IPv4 そして IPv6 は長さが異なります。

バイト演算はすべての IP が 4 オクテットであることに依存するため、推奨されません。

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になります

私はクリーンな解決策を見つけたことがありません(つまり:この問題については、.NET Framework のクラス/メソッド) を使用してください。あなたが提供した解決策/例、またはAkuの例以外は利用できないと思います。:(

完全なソリューション:

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top