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.

``````Function ConvertIPToBinary(strIP)
' Converts an IP Address into Binary

Dim arrOctets : arrOctets = Split(strIP, ".")

Dim i
For i = 0 to UBound(arrOctets)
Dim intOctet : intOctet = CInt(arrOctets(i))
Dim strBinOctet : strBinOctet = ""

Dim j
For j = 0 To 7
If intOctet And (2^(7 - j)) Then
strBinOctet = strBinOctet & "1"
Else
strBinOctet = strBinOctet & "0"
End If
Next arrOctets(i) = strBinOctet
Next

ConvertIPToBinary = Join(arrOctets, ".")
End Function
``````

### 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.

``````Function ConvertBinIPToDecimal(strBinIP)
' Convert binary form of an IP back to decimal

Dim arrOctets : arrOctets = Split(strBinIP, ".")
Dim i
For i = 0 to UBound(arrOctets)
Dim intOctet : intOctet = 0

Dim j
For j = 0 to 7
Dim intBit : intBit = CInt(Mid(arrOctets(i), j + 1, 1))
If intBit = 1 Then
intOctet = intOctet + 2^(7 - j)
End If
Next
arrOctets(i) = CStr(intOctet)
Next

ConvertBinIPToDecimal = Join(arrOctets, ".")
End Function
``````

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

``````Function MaskLength(strMask)

Dim arrOctets : arrOctets = Split(strMask, ".")
Dim i
For i = 0 to UBound(arrOctets)
Dim intOctet : intOctet = CInt(arrOctets(i))
For j = 0 To 7
If intOctet And (2^(7 -j)) Then
End If
Next
Next

End Function
``````

``````Function MaskLengthToIP(intMask)

Dim arrOctets(3)
Dim intFullOctets : intFullOctets = (intMask - (intMask Mod 8)) / 8
Dim i
For i = 0 To (intFullOctets - 1)
arrOctets(i) = "255"
Next

Dim intPartialOctetLen : intPartialOctetLen = intMask Mod 8
Dim j
If intPartialOctetLen > 0 Then
Dim intOctet
For j = 0 To (intPartialOctetLen - 1)
intOctet = intOctet + 2^(7 - j)
Next
arrOctets(i) = intOctet : i = i + 1
End If

For j = i To 3
arrOctets(j) = "0"
Next

End Function
``````

### 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.

``````Function CalcNetworkAddress(strIP, strMask)
' Conversion of IP and Mask to binary

Dim strBinIP : strBinIP = ConvertIPToBinary(strIP)

' Bitwise AND operation (except for the dot)
Dim i, strBinNetwork
For i = 1 to Len(strBinIP)
Dim strIPBit : strIPBit = Mid(strBinIP, i, 1)

If strIPBit = "1" And strMaskBit = "1" Then
strBinNetwork = strBinNetwork & "1"
ElseIf strIPBit = "." Then
strBinNetwork = strBinNetwork & strIPBit
Else
strBinNetwork = strBinNetwork & "0"
End If
Next

' Conversion of Binary IP to Decimal
End Function
``````

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.

``````Function CalcBroadcastAddress(strIP, strMask)
' Conversion of IP and Mask to binary

Dim strBinIP : strBinIP = ConvertIPToBinary(strIP)

' Set each unmasked bit to 1
For i = 1 to Len(strBinIP)
Dim strIPBit : strIPBit = Mid(strBinIP, i, 1)