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

22,594

Solution 1

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.

Solution 2

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

?

Solution 3

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#?

Solution 4

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

Solution 5

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

Share:
22,594
Hershi
Author by

Hershi

Hmmmm...

Updated on September 19, 2020

Comments

  • Hershi
    Hershi almost 4 years

    I'm writing C# code that uses the windows IP Helper API. One of the functions I'm trying to call is "GetBestInterface" that takes a 'uint' representation of an IP. What I need is to parse a textual representation of the IP to create the 'uint' representation.

    I've found some examples via Google, like this one or this one, but I'm pretty sure there should be a standard way to achieve this with .NET. Only problem is, I can't find this standard way. IPAddress.Parse seems to be in the right direction, but it doesn't supply any way of getting a 'uint' representation...

    There is also a way of doing this using IP Helper, using the ParseNetworkString, but again, I'd rather use .NET - I believe the less I rely on pInvoke the better.

    So, anyone knows of a standard way to do this in .NET?

  • mjv
    mjv over 14 years
    BitConvertor.ToUINt32() takes two parameters (see msdn.microsoft.com/en-us/library/…)
  • Andrei Rînea
    Andrei Rînea over 14 years
    Are you sure about the four indices' order? it seems to me that byte with index 0 should be shifted 24 bits and not the one with index 3..
  • Popeye
    Popeye about 11 years
    Whoever tried to edit this answer please don't try editing code, even if incorrect because of typo just add a comment to inform the user who the answer belongs to, and don't go deleting the rest of the answer to just say why you did it.
  • Janis Veinbergs
    Janis Veinbergs almost 2 years
    Or instead of last 4 lines, use: BitConverter.ToUInt32(ipBytes, 0) See this fiddle on how to convert it to use network byte order that increments naturally as IP addresses goes: dotnetfiddle.net/eZguXl