Important alert: (current site time 10/31/2014 6:13:58 AM EDT)
 

VB icon

Converting an IP address from a String value to a Long integer, and back

Email
Submitted on: 10/8/2006 4:18:44 AM
By: Remus Oproiu Vasii 
Level: Beginner
User Rating: By 1 Users
Compatibility: C#, VB.NET, ASP.NET
Views: 12193
 
     Converting an Internet IP address from its standard notation to an integer (and back) is quite simple in .NET -- but finding a quick example on the Internet on how to do it seems to be very dificult. So search no more, here is the code...
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Converting an IP address from a String value to a Long integer, and back
// Description:Converting an Internet IP address from its standard notation to an integer (and back) is quite simple in .NET -- but finding a quick example on the Internet on how to do it seems to be very dificult. So search no more, here is the code...
// By: Remus Oproiu Vasii
//
// Side Effects:Observe that this example is compatible with IPv4 only. Actually Long variables should be used instead of Integer since the System.Net.IPAddress class uses only Long variables (for IPv6 addresses). But the code can be easily adjusted, so I leave that to you.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5179&lngWId=10//for details.//**************************************

Public Function IPToInteger(ByVal Expression As String) As Integer
	Try
		Dim IPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(Expression)
		With IPAddress
			Return (System.Convert.ToInt32(.GetAddressBytes(3)) << 24) Or (System.Convert.ToInt32(.GetAddressBytes(2)) << 16) Or (System.Convert.ToInt32(.GetAddressBytes(1)) << 8) Or System.Convert.ToInt32(.GetAddressBytes(0))
		End With
	Catch ex As Exception
		Return 0I
	End Try
End Function
Public Function IPToString(ByVal Expression As Integer) As String
	Try
		Dim IPAddress As System.Net.IPAddress = New System.Net.IPAddress(System.Convert.ToInt64(Expression))
		Return IPAddress.ToString
	Catch ex As Exception
		Return "0.0.0.0"
	End Try
End Function


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/22/2007 11:27:46 AMkarthi

nicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenicenice nicenicenicenicenice
(If this comment was disrespectful, please report it.)

 
9/1/2009 9:14:55 AMJoel

How would you use this?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.