Jun 3, 2012

How to Convert IPv4 Address into integer and vice-versa?

1 comment
Some times it may be required that, application will get IP Address as integer and you need to use this for further processing. This is very simple and let's see how to do,



// IP Adress to Integer 74.125.236.84 -> 1424784714
int intAddress = BitConverter.ToInt32(IPAddress.Parse(txtIPAddress.Text).GetAddressBytes(), 0);
            MessageBox.Show(string.Format("IP Address {0} equilant to integer: {1}", txtIPAddress.Text, intAddress));


// Integer to IPAddress
string ipAddress = new IPAddress(BitConverter.GetBytes(intAddress)).ToString(); 
MessageBox.Show(string.Format("Integer {0} equialant to IP Address :{1}", intAddress.ToString(), ipAddress));

 Where, txtIPAddress.Text = valid ip address like [74.125.236.84].

Note: Need to include directive using System.Net; to get IPAddress

1 comment :