Convert StringIP Address to LongIP Address

Converts a string ip address ("") 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 ("") 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.

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)
         ' 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

Convert StringIP to LongIP asp IP storage

Back To Top
© 1998 - 2024
Version 7.21 | Advertise on this site