How do you parse an IP address string to a uint value in C#?

穿精又带淫゛_ 提交于 2019-11-30 09:59:36

MSDN says that IPAddress.Address property (which returns numeric representation of IP address) is obsolete and you should use GetAddressBytes method.

You can convert IP address to numeric value using following code:

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

EDIT:
As other commenters noticed above-mentioned code is for IPv4 addresses only. IPv6 address is 128 bits long so it's impossible to convert it to 'uint' as question's author wanted.

Shouldn't it be:

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

?

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

This solution is easier to read than manual bit shifting.

See How to convert an IPv4 address into a integer in C#?

Also you should remember that IPv4 and IPv6 are different lengths.

Byte arithmetic is discouraged, as it relies on all IPs being 4-octet ones.

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

Output will be 192 168 1 1

Correct solution that observes Endianness:

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

I have never found a clean solution (i.e.: a class / method in the .NET Framework) for this problem. I guess it just isn't available except the solutions / examples you provided or Aku's example. :(

Complete solution:

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();
}

Reverse order of bytes:

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();
}

You can test here:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!