0

Possible Duplicates:
.NET Equivalant for INET_NTOA and INET_ATON
How to convert an IPv4 address into a integer in C#?

How do I perform the same unix function inet_aton in c#?

Community
  • 1
  • 1
001
  • 55,049
  • 82
  • 210
  • 324

3 Answers3

3

Check out the IPAddress.Parse (or TryParse) methods of the IPAddress class.

An example would be:

static int IPStringToInt(string ipAddress)
{
    IPAddress address = IPAddress.Parse(ipAddress);
    byte[] asBytes = address.GetAddressBytes();

    if(asBytes.Length != 4)
    {
        throw new ArgumentException("IP Address must be an IPv4 address");
    }

    return BitConverter.ToInt32(asBytes, 0);
}

You will need to take into account the host and network order of the bytes, but there's several static methods on the IPAddress class for handling that.

Joshua Rodgers
  • 5,018
  • 2
  • 28
  • 29
2
public int ToInteger(int A, int B, int C, int D)
{
    return Convert.ToInt32((A* Math.Pow(256, 3)) + (B* Math.Pow(256, 2)) + (C* 256) + D);
}

or http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse.aspx

you can extend your own IP class like I did.

Kubi
  • 2,019
  • 6
  • 32
  • 59
-1

Try this i think is the same question and it has an answer (enjoy)

Community
  • 1
  • 1
cristian
  • 8,300
  • 3
  • 35
  • 44