VB icon

Convert StringIP to NumericIP

Email
Submitted on: 1/5/2015 8:25:00 AM
By: Lewis E. Moten III (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 1672
 
     Converts a string ip address ("192.168.0.1") to a Long number (3232235521). One of the resons to do this would be to store IP addresses in databases. Numbers greatly reduce the size required to store this information.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Convert StringIP to NumericIP
'**************************************
Copyright (C) 1999, Lewis Moten. All rights reserved.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Convert StringIP to NumericIP
' Description:Converts a string ip address ("192.168.0.1") to a Long number (3232235521). One of the resons to do this would be to store IP addresses in databases. Numbers greatly reduce the size required to store this information.
' By: Lewis E. Moten III (from psc cd)
'
' Inputs:asNewIP - String IP address ("192.168.0.1") to convert to a number.
'
' Returns:Returns a Long Integer representing the IP address (3232235521)
'
' Assumes:This function assumes that your IP address has 4 integers delimited by decimals and that the numbers range from 0 to 255.
'
' Side Effects:If storing number within an Access Database, The variable type "Long" ranges from -2147483648 to 2147483647. You will need to subtract 2147483648 from the number before querying the database.
'**************************************

Function CLngIP(ByVal asNewIP)
	Dim lnResults
	Dim lnIndex
	Dim lnIpAry
	' Split the IP address using the dot as a delimiter
	lnIpAry = Split(asNewIP, ".", 4)
	' Loop through each number in the IP address
	For lnIndex = 0 To 3
		
		' If we are not working with the last number...
		If Not lnIndex = 3 Then
			
			' Convert the number to a value range that can be parsed from the others
			lnIpAry(lnIndex) = lnIpAry(lnIndex) * (256 ^ (3 - lnIndex))
		
		End If
		
		' Add the number to the results
		lnResults = lnResults + lnIpAry(lnIndex)
	
	Next
	' If storing number within an Access Database,
	' The variable type "Long" ranges from -2147483648 to 2147483647
	' You will need to subtract 2147483648 from the number
	' before querying the database.
	
	' lnResults = lnResults - 2147483648
	' Return the results
	CLngIP = lnResults
End Function


Other 63 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 Advanced 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


 There are no comments on this submission.
 

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.