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
```

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

```
Function MaskLength(strMask)
' Converts an subnet mask into a mask length in bits
Dim arrOctets : arrOctets = Split(strMask, ".")
Dim i
For i = 0 to UBound(arrOctets)
Dim intOctet : intOctet = CInt(arrOctets(i))
Dim j, intMaskLength
For j = 0 To 7
If intOctet And (2^(7 -j)) Then
intMaskLength = intMaskLength + 1
End If
Next
Next
MaskLength = intMaskLength
End Function
```

### Convert a mask length to a subnet mask

```
Function MaskLengthToIP(intMask)
' Converts a mask length to the decimal format mask
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
MaskLengthToIP = Join(arrOctets, ".")
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)
' Generates the Network Address from the IP and Mask
' Conversion of IP and Mask to binary
Dim strBinIP : strBinIP = ConvertIPToBinary(strIP)
Dim strBinMask : strBinMask = ConvertIPToBinary(strMask)
' Bitwise AND operation (except for the dot)
Dim i, strBinNetwork
For i = 1 to Len(strBinIP)
Dim strIPBit : strIPBit = Mid(strBinIP, i, 1)
Dim strMaskBit : strMaskBit = Mid(strBinMask, 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
CalcNetworkAddress= ConvertBinIPToDecimal(strBinNetwork)
End Function
```

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

```
Function CalcBroadcastAddress(strIP, strMask)
' Generates the Broadcast Address from the IP and Mask
' Conversion of IP and Mask to binary
Dim strBinIP : strBinIP = ConvertIPToBinary(strIP)
Dim strBinMask : strBinMask = ConvertIPToBinary(strMask)
' Set each unmasked bit to 1
Dim i, strBinBroadcast
For i = 1 to Len(strBinIP)
Dim strIPBit : strIPBit = Mid(strBinIP, i, 1)
Dim strMaskBit : strMaskBit = Mid(strBinMask, i, 1)
If strIPBit = "1" Or strMaskBit = "0" Then
strBinBroadcast = strBinBroadcast & "1"
ElseIf strIPBit = "." Then
strBinBroadcast = strBinBroadcast & strIPBit
Else
strBinBroadcast = strBinBroadcast & "0"
End If
Next
' Conversion of Binary IP to Decimal
CalcBroadcastAddress = ConvertBinIPToDecimal(strBinBroadcast) End Function
```