Convert an IP address string to an integer

10/19/2010 8:14:34 PM

IP addresses are really 4 byte integers. This function converts an IP string like "192.168.0.1" into its numerical equivalent.


public int IPAddressToNumber(string IPaddress)
{
        string []  arrDec
        int num = 0;
        if (IPaddress != "")
        {
                arrDec =  IPaddress.Split('.');
                num = (int.Parse(arrDec[3])) +  (int.Parse(arrDec[2]) * 256) + (int.Parse(arrDec[1]) * 65536) +  (int.Parse(arrDec[0]) * 16777216);
        }
        return num;
}