At times it can be very convenient to be able to work with IP Addresses and subnets in VbScript. This collection of functions handles subnet maths in VbScript.

A PowerShell version of these functions can be found here.

Convert an IP to binary

This function performs a bitwise comparison (AND) with each octet in the original IP to determine if each power of two is present starting with the highest, 128 returning the binary version of any IP address.

Convert a binary IP to a decimal IP

It is important to be able to convert back into the familiar form of the IP address, this calculates the decimal form of an IP address based on the binary.

Convert a subnet mask to a mask length

Occasionally it is desirable to calculate the subnet mask bit length. This can be done using the following.

Convert a mask length to a subnet mask

Calculate the subnet network address

The functions above can be used with along with a bitwise AND operation against an IP address and subnet mask to calculate the network address.

Note that this function includes calls to both ConvertIPToBinary and ConvertBinIPToDecimal.

Calculate the subnet broadcast address

To calculate the broadcast address requires a modification of the CalcNetworkAddress function above. This time, it sets anything that is not covered by the mask to 1.

Note that this function includes calls to both ConvertIPToBinary and ConvertBinIPToDecimal.


  1. This is all very impressive. I am able to get most of these to work for what I need, which is fantastic. However, I cannot for the life of me get the ‘Calculate the subnet network address’ function(s) to work.

    It just returns a string like so;

    00001010.00011110.00110011.00111110 11111111.11111111.11111111.00000000 0 0 00001010.00011110.00110011.00000000

    I am trying to get the network address (e.g. 192.168.0/24) from the IP address and Subnet mask;

    strIP = “”
    strMask = “”

    CalcNetworkAddress strIP, strMask

    I am sure I am missing something.

    TIA !


  2. Thanks a lot for these scripts. I them in one of my script.

    I have written two extra functions (based on yours), which returns the first and last IP address of a subnet. (usefull for some tools).

    I place my contribution in public domain.
    BTW, Could you clarify the license of your script (typically Public domain, BSD license, or GPL license).


  3. Hey Franklin,

    Thanks for the contribution ๐Ÿ™‚

    All code is intended to be public domain, I’ll post an explicit statement to that effect somewhere.



  4. Hi Jon,

    Sorry for the delay in getting back to you, I’ve been on holiday so a bit out of touch.

    The functions return the correct value for me, did you also include the ConvertIPToBinary and ConvertBinIPToDecimal?

    Normally you would use the function something like this:

    strNetAddr = CalcNetworkAddress(strIP, strMask)
    WScript.Echo CalcNetworkAddress(strIP, strMask)



  5. Thanks for these functions. I used them, and the information on some of your other pages to write the following functions:


  6. Realy great collection of scripts.
    Right the scripts I was looking for.


  7. Great work. functions work perfectly and have saved me a heap of work.

    Awesome ๐Ÿ™‚


  8. Beautiful functions!
    Thumbs up!



  9. If someone is looking for a very fast code to do some IP- and subnet calculations via vbscript, then I would recommend my post here:



    • Good work. Bit-operations are certainly the way to go, I did the same for the PowerShell version of these functions.



  10. Very nice. Just what I needed. Thanks so much for sharing!!!


  11. The snippets on this post are a God-send, thanks a lot.

    Bellow are two quick wrapper functions I added for ease of use (see below for a sample use):

    [code language=”vb”]
    function IpToNumber(sIP)
    ‘Converts an IP-string into a large positive number.
    dim octet
    for each octet in split(sIP, ".")
    IpToNumber = IpToNumber * 256 + CByte(octet)
    end function

    function isIpInRange(sIP, sIpFrom, sIpTo)
    dim lTarget

    isIpInRange = false

    lTarget = IpToNumber(sIP)
    if lTarget < IpToNumber(sIpFrom) then exit function
    if lTarget > IpToNumber(sIpTo) then exit function

    isIpInRange = true
    end function

    For example, say you need to test if is contained in
    First you need to translate into the upper and lower bounds, then see if the target IP is in range:

    [code language=”vb”]
    sTargetIP = ""
    sMaskLength = "22"

    bFound = isIpInRange(sTargetIP, CalcRangeStart(sRangeIP, MaskLengthToIP(sMaskLength)), CalcRangeEnd(sRangeIP, MaskLengthToIP(sMaskLength))))


  12. Pingback: IP Blocklist to Cisco ASA access-list VBscript | thejimmahknows

Leave a Reply

Your email address will not be published. Required fields are marked *