Convert IP Address into int, (ie inet aton in c#) [duplicate]
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 th开发者_如何学Pythone same unix function inet_aton in c#?
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.
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.
Try this i think is the same question and it has an answer (enjoy)
精彩评论