Converts an IP address to a long integer and converts a long integer to an IP address

Source: Internet
Author: User
Tags integer range ranges split variable access database
IP Address | Convert IP address to Long integer

converts a string IP address ("192.168.0.1") to a Long number (3232235521). One of the reasons to does this would is 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.





Returns:returns a Long Integer representing the IP address (3232235521)





Assumes:this function assumes that your IP addresses has 4 integers delimited by decimals and that numbers range F Rom 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 into 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 of that can is 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'll need to subtract 2147483648 from the number


' before querying the database.





' lnresults = lnResults-2147483648


' Return the results


CLNGIP = Lnresults


End Function





converts a long integer to an IP address





Name:convert Longip to Stringip


The Description:this function converts a Long number (3232235521) into the IP address ("192.168.0.1"). Why would you want to doing this? Click here.


Inputs:annewip-ip Address as a Long (no dots)





Returns:returns The string representation of a IP address ("192.168.0.1")





Function Cstrip (ByVal annewip)


Dim lsresults ' Results to be returned


Dim lntemp ' temporary value being parsed


Dim Lnindex ' Position of number being parsed


' If pulling number from an Access Database,


' the variable Type ' Long ' ranges from-2147483648 to 2147483647


' You'll-need to add 2147483648 to the # to parse correctly.


' Annewip = Annewip + 2147483648


' Parse highest numbers-A-


for lnindex = 3 to 0 Step-1





' Parse The current value of this position


Lntemp = Int (Annewip/(256 ^ lnindex))





' Append the number to the final results delimited by a dot


lsresults = lsresults & Lntemp & "."





' Remove The number that we just parsed


Annewip = Annewip-(lntemp * (256 ^ lnindex))





Next





' cut off last dot


Lsresults = Left (Lsresults, Len (lsresults)-1)





' Return the results


Cstrip = Lsresults





End Function





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.