Home-->
Articles -->
Site Tools
Converts a string ip address to a Long number

Post By fulltilthous on 2008-9-19 15:16:16 [Reads:300]
Converts a string ip address ("192.168.0.1") to a Long number (3232235521). One of the reasons to do this would be to store IP addresses in databases. Numbers greatly reduce the size required to store this information.
Inputs: asNewIP - String IP address ("192.168.0.1") to convert to a number.
ASP code
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.
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 #118alue 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