Conversion between IP address point notation and decimal notation

Source: Internet
Author: User
Tags ip number
'

' Convert IP address point-to-point to decimal.

'

Public   Shared   Function dot2longip () Function Dot2longip ( Byval Dotip As   String ) As   Long

' Use regular expressions to check IP addresses

Dim Subip As   String () =   Split (Dotip, " . " )

' IP address = W. x. y. Z

' IP number = 16777216 * w + 65536 * x + 256 * Y + z



Dot2longip =   16777216   *   Clng (Subip ( 0 )) +   65536   *   Clng (Subip ( 1 )) +   256   *   Clng (Subip ( 2 )) +   Clng (Subip ( 3 ))


End Function


Public   Shared   Function longip2dot () Function Longip2dot ( Byval Longip As   Long ) As   String

' IP address = W. x. y. Z

' IP number = 16777216 * w + 65536 * x + 256 * Y + z



' W = int (IP number/16777216) % 256

' X = int (IP number/65536) % 256

' Y = int (IP number/256) % 256

' Z = int (IP number) % 256



Dim Dotip As   String

Dim Subip As   Integer


Subip =   CINT (Fix (longip /   16777216) ) MoD   256

Dotip =   CSTR (Subip) +   " ."



Subip =   CINT (Fix (longip /   65536) ) MoD   256

Dotip + =   CSTR (Subip) +   " ."



Subip =   CINT (Fix (longip /   256) ) MoD   256

Dotip + =   CSTR (Subip) +   " ."



Subip =   CINT (Fix (longip MoD   256) )

Dotip + =   CSTR (Subip)


Return Dotip

End Function

In addition, ninputer: The method is indeed good, and the efficiency must be higher than the previous calculation method.
< Structlayout (layoutkind. Explicit ) > _

Public   Structure ipconvert Structure Ipconvert

< Fieldoffset ( 0 ) >   Public Longip As   Long  

< Fieldoffset ( 0 ) >   Public Dotip0 As   Byte  

< Fieldoffset ( 1 ) >   Public Dotip1 As   Byte  

< Fieldoffset ( 2 ) >   Public Dotip2 As   Byte  

< Fieldoffset ( 3 ) >   Public Dotip3 As   Byte  

End Structure  

Note that the namespace system. runtime. interopservices must be introduced, and the longip type is indeed long, not integer.

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.