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

I am writing C # code that uses the IP Helper API. One of the functions I'm trying to name is " GetBestInterface ", which accepts a "uint" IP representation. I need to parse the IP text view to create the uint view.

I found some examples through Google, for example this or this , but I am sure that for .NET this should be a standard way. The only problem is that I can not find this standard way. IPAddress.Parse seems to be in the right direction, but it does not provide any way to get the "uint" view ...

There is also a way to do this using IP Helper using ParseNetworkString , but again I would rather use .NET - I believe that the less I rely on pInvoke, the better.

So, does anyone know a standard way to do this in .NET?

+6
c # winapi networking
source share
9 answers

MSDN says that the IPAddress.Address property (which returns a numeric representation of the IP address) is deprecated, and you should use GetAddressBytes .

You can convert the IP address to a numeric value using the 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 commentators noted, the above code is only for IPv4 addresses. The IPv6 address is 128 bits long, so it is impossible to convert it to "uint", as the author of the question requires.

+11
source share

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

?

+17
source share
 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 to an integer in C #?

+10
source share

You should also remember that IPv4 and IPv6 are different lengths.

+5
source share

Byte arithmetic is not recommended, as it relies on all IP addresses that are 4-octet.

+1
source share
 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]); 

The output will be 192 168 1 1

+1
source share

The correct solution is byte ordering:

 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]; } 
+1
source share

I did not find a clean solution for this problem (i.e.: class / method in the .NET Framework). I think this is simply not available, except for the solutions / examples or Aku example you submitted. :(

0
source share

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 byte order:

 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 check 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

0
source share

All Articles