<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indented! &#187; IP Conversion</title>
	<atom:link href="http://www.indented.co.uk/index.php/tag/ip-conversion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indented.co.uk</link>
	<description></description>
	<lastBuildDate>Mon, 17 Oct 2011 19:03:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>IPv4 subnet math with VbScript</title>
		<link>http://www.indented.co.uk/index.php/2008/10/21/vbscript-subnet-math/</link>
		<comments>http://www.indented.co.uk/index.php/2008/10/21/vbscript-subnet-math/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 12:23:55 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[VbScript]]></category>
		<category><![CDATA[IP Conversion]]></category>
		<category><![CDATA[Subnet Math]]></category>
		<category><![CDATA[vbs]]></category>

		<guid isPermaLink="false">http://www.highorbit.co.uk/?p=270</guid>
		<description><![CDATA[At times it can be very convenient to be able to work with IP Addresses and subnetting in VbScript. This collection of functions handles subnet math 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 [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>At times it can be very convenient to be able to work with IP Addresses and subnetting in VbScript. This collection of functions handles subnet math in VbScript.</p>
<p>A PowerShell version of these functions can be found <a href='http://www.indented.co.uk/index.php/2010/01/23/powershell-subnet-math/'>here</a>.<br />
<span id="more-270"></span></p>
<h3>Convert an IP to binary</h3>
<p>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.</p>
<pre class="brush: vb; title: ; notranslate">
Function ConvertIPToBinary(strIP)
  ' Converts an IP Address into Binary

  Dim arrOctets : arrOctets = Split(strIP, &quot;.&quot;)
  Dim i
  For i = 0 to UBound(arrOctets)
    Dim intOctet : intOctet = CInt(arrOctets(i))
    Dim strBinOctet : strBinOctet = &quot;&quot;
    Dim j
    For j = 0 To 7
      If intOctet And (2^(7 - j)) Then
        strBinOctet = strBinOctet &amp; &quot;1&quot;
      Else
        strBinOctet = strBinOctet &amp; &quot;0&quot;
      End If
    Next
    arrOctets(i) = strBinOctet
  Next
  ConvertIPToBinary = Join(arrOctets, &quot;.&quot;)
End Function
</pre>
<h3>Convert a binary IP to a decimal IP</h3>
<p>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.</p>
<pre class="brush: vb; title: ; notranslate">
Function ConvertBinIPToDecimal(strBinIP)
  ' Convert binary form of an IP back to decimal

  Dim arrOctets : arrOctets = Split(strBinIP, &quot;.&quot;)
  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, &quot;.&quot;)
End Function
</pre>
<h3>Convert a subnet mask to a mask length</h3>
<p>Occasionally it is desirable to calculate the subnet mask bit length. This can be done using the following.</p>
<pre class="brush: vb; title: ; notranslate">
Function MaskLength(strMask)
  ' Converts an subnet mask into a mask length in bits

  Dim arrOctets : arrOctets = Split(strMask, &quot;.&quot;)
  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
</pre>
<h3>Convert a mask length to a subnet mask</h3>
<pre class="brush: vb; title: ; notranslate">
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) = &quot;255&quot;
  Next

  Dim intPartialOctetLen : intPartialOctetLen = intMask Mod 8
  Dim j
  If intPartialOctetLen &gt; 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) = &quot;0&quot;
  Next

  MaskLengthToIP = Join(arrOctets, &quot;.&quot;)
End Function
</pre>
<h3>Calculate the subnet network address</h3>
<p>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.</p>
<p>Note that this function includes calls to both ConvertIPToBinary and ConvertBinIPToDecimal.</p>
<pre class="brush: vb; title: ; notranslate">
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 = &quot;1&quot; And strMaskBit = &quot;1&quot; Then
      strBinNetwork = strBinNetwork &amp; &quot;1&quot;
    ElseIf strIPBit = &quot;.&quot; Then
      strBinNetwork = strBinNetwork &amp; strIPBit
    Else
      strBinNetwork = strBinNetwork &amp; &quot;0&quot;
    End If
  Next

  ' Conversion of Binary IP to Decimal
  CalcNetworkAddress= ConvertBinIPToDecimal(strBinNetwork)
End Function
</pre>
<h3>Calculate the subnet broadcast address</h3>
<p>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.</p>
<p>Note that this function includes calls to both ConvertIPToBinary and ConvertBinIPToDecimal.</p>
<pre class="brush: vb; title: ; notranslate">
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 = &quot;1&quot; Or strMaskBit = &quot;0&quot; Then
      strBinBroadcast = strBinBroadcast &amp; &quot;1&quot;
    ElseIf strIPBit = &quot;.&quot; Then
      strBinBroadcast = strBinBroadcast &amp; strIPBit
    Else
      strBinBroadcast = strBinBroadcast &amp; &quot;0&quot;
    End If
  Next

  ' Conversion of Binary IP to Decimal
  CalcBroadcastAddress = ConvertBinIPToDecimal(strBinBroadcast)
End Function
</pre>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2008/10/21/vbscript-subnet-math/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

