CIDR to Subnet in VB.NET2010http://forums.asp.net/t/1799818.aspx/1?CIDR+to+Subnet+in+VB+NET2010Mon, 07 May 2012 16:46:05 -040017998184964315http://forums.asp.net/p/1799818/4964315.aspx/1?CIDR+to+Subnet+in+VB+NET2010CIDR to Subnet in VB.NET2010 <p>How can I replicate <a target="_blank" href="http://www.tech-faq.com/convert-cidr-to-netmask"> this</a> in VB.NET 2010 in Framework 4?</p> <p>Have searched for this code for over 5 hours and not one site has this code. There are a few that do subnet to cidr but that isn't the way I wish it to go. There is also on the net a VB6 project converted to vb.net but again it doesn't do what I want so pointless linking me to try</p> <p>No Google links please as they will be ignored. If all you do is post links them please ruin another thread</p> <p>Thanks in advance</p> 2012-05-03T18:28:21-04:004964507http://forums.asp.net/p/1799818/4964507.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 <p>Try this :&nbsp;</p> <pre id="code-result" class="brush:vbnet">Private Shared Sub Network2IpRange(sNetwork As String, ByRef startIP As UInteger, ByRef endIP As UInteger) ' ip address ' subnet mask ' Broadcast address Dim ip As UInteger, mask As UInteger, broadcast As UInteger, network As UInteger ' Network address Dim bits As Integer Dim elements As String() = sNetwork.Split(New [Char]() {&quot;/&quot;C}) ip = IP2Int(elements(0)) bits = Convert.ToInt32(elements(1)) mask = Not (&amp;HffffffffUI &gt;&gt; bits) network = ip And mask broadcast = network &#43; Not mask usableIps = If((bits &gt; 30), 0, (broadcast - network - 1)) If usableIps &lt;= 0 Then startIP = InlineAssignHelper(endIP, 0) Else startIP = network &#43; 1 endIP = broadcast - 1 End If End Sub Public Shared Function IP2Int(IPNumber As String) As UInteger Dim ip As UInteger = 0 Dim elements As String() = IPNumber.Split(New [Char]() {&quot;.&quot;C}) If elements.Length = 4 Then ip = Convert.ToUInt32(elements(0)) &lt;&lt; 24 ip &#43;= Convert.ToUInt32(elements(1)) &lt;&lt; 16 ip &#43;= Convert.ToUInt32(elements(2)) &lt;&lt; 8 ip &#43;= Convert.ToUInt32(elements(3)) End If Return ip End Function</pre> 2012-05-03T22:42:41-04:004965405http://forums.asp.net/p/1799818/4965405.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 <p>Hi,</p> <p>Thanks for the code but what I wish to do is replicate the webpage in my original post. Jpye a CIDR, pre4ss the button for the result. That means no start address, no end address etc. If you look at the Javascript file it uses there are 3 functions and when trying to convert that to VB.NET I must be doing something wrong because on a /24 I am returning 64.64.64.0 not 255.255.255.0 and instead of returning the value to 4 textboxes like on that webpage, I wish to return as a string. As there are no Javascript to VB.NET convertors out there and my Java programming not being used for 12-14 years (Visual J&#43;&#43; in Visual Studio 98) I have made an error</p> <p>In your example the start and the end IP's will be the same as I am not interested in using them whatsoever. That's why I gave a link to that page because this is EXACTLY what I wish to replicate. If you look at the comments in your example it says IP ADDRESS, SUBNET MASK, BROADCAST ADDRESS. I don't know the subnet as this is what I am wanting to calculate (see original post)</p> <p>Any ideas on how I can achieve my original goal?</p> 2012-05-04T10:30:46-04:004967940http://forums.asp.net/p/1799818/4967940.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 <p>Sorry I posted from related code, but not the correct one.&nbsp;</p> <p>Try this one, it takes input 192.168.0.1/25 and outputs&nbsp;192.168.0.1 - 192.168.0.126</p> <p><span><br> </span></p> <pre class="lang-cs prettyprint"><pre class="prettyprint">IPAddress ip = new IPAddress(new byte[] { 192, 168, 0, 1 }); int bits = 25; uint mask = ~(uint.MaxValue &gt;&gt; bits); // Convert the IP address to bytes. byte[] ipBytes = ip.GetAddressBytes(); // BitConverter gives bytes in opposite order to GetAddressBytes(). byte[] maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray(); byte[] startIPBytes = new byte[ipBytes.Length]; byte[] endIPBytes = new byte[ipBytes.Length]; // Calculate the bytes of the start and end IP addresses. for (int i = 0; i &lt; ipBytes.Length; i&#43;&#43;) { startIPBytes[i] = (byte)(ipBytes[i] &amp; maskBytes[i]); endIPBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]); } // Convert the bytes to IP addresses. IPAddress startIP = new IPAddress(startIPBytes); IPAddress endIP = new IPAddress(endIPBytes);</pre></pre> <pre class="lang-cs prettyprint"><pre class="prettyprint"><span class="pun"><br /></span></pre></pre></pre> 2012-05-07T00:00:02-04:004967942http://forums.asp.net/p/1799818/4967942.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 <p>Sorry whilst the above is again very close you what you want it isn't exactly what you asked for.&nbsp;</p> <p></p> <p>I believe that the below will do what you want. (shamelessly lifted from&nbsp;<a href="http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx">http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx</a>)</p> <p></p> <pre class="code"> <span>public static </span><span>IPAddress </span>CreateByHostBitLength(<span>int </span>hostpartLength) { <span>int </span>hostPartLength = hostpartLength; <span>int </span>netPartLength = 32 - hostPartLength; <span>if </span>(netPartLength &lt; 2) <span>throw new </span><span>ArgumentException</span>(<span>&quot;Number of hosts is to large for IPv4&quot;</span>); <span>Byte</span>[] binaryMask = <span>new byte</span>[4]; <span>for </span>(<span>int </span>i = 0; i &lt; 4; i&#43;&#43;) { <span>if </span>(i * 8 &#43; 8 &lt;= netPartLength) binaryMask[i] = (<span>byte</span>)255; <span>else if </span>(i * 8 &gt; netPartLength) binaryMask[i] = (<span>byte</span>)0; <span>else </span>{ <span>int </span>oneLength = netPartLength - i * 8; <span>string </span>binaryDigit = <span>String</span>.Empty.PadLeft(oneLength, <span>'1'</span>).PadRight(8, <span>'0'</span>); binaryMask[i] = <span>Convert</span>.ToByte(binaryDigit, 2); } } <span>return new </span><span>IPAddress</span>(binaryMask); } <span>public static </span><span>IPAddress </span>CreateByNetBitLength(<span>int </span>netpartLength) { <span>int </span>hostPartLength = 32 - netpartLength; <span>return </span>CreateByHostBitLength(hostPartLength); } <span>public static </span><span>IPAddress </span>CreateByHostNumber(<span>int </span>numberOfHosts) { <span>int </span>maxNumber = numberOfHosts &#43; 1; <span>string </span>b = <span>Convert</span>.ToString(maxNumber, 2); <span>return </span>CreateByHostBitLength(b.Length); }</pre> 2012-05-07T00:05:40-04:004967943http://forums.asp.net/p/1799818/4967943.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 Hi, Thanks for the replay. I am not interested in calculating the range. Please click the link in my original post, look at the what it does then you'll see exatly what I am trying to do If you vie source, ctrl &#43; f typing button you will see a javascript file link above. In that file are the 3 functions needed to do the conversion. I am trying to convert the calculations into vb.net Any ideas on code to replicate that webpage? Thanks in advance 2012-05-07T00:08:38-04:004967945http://forums.asp.net/p/1799818/4967945.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 The C# code isn't what I am looking for. For instructions see my original post and my previous reply I want to be able to type '24' for example to return 255.255.255.0 etc 2012-05-07T00:11:38-04:004968024http://forums.asp.net/p/1799818/4968024.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 <p>something like this:</p> <pre class="prettyprint">Public Function ConvertCIDRToMask(cidr As Integer) As String Dim ip As Int64 = Convert.ToInt64(New String(&quot;1&quot;c, cidr).PadRight(32, &quot;0&quot;c), 2) Return (String.Join(&quot;.&quot;, New Net.IPAddress(ip).GetAddressBytes.Reverse)) End Function</pre> <p><br> <br> </p> 2012-05-07T01:15:18-04:004969511http://forums.asp.net/p/1799818/4969511.aspx/1?Re+CIDR+to+Subnet+in+VB+NET2010Re: CIDR to Subnet in VB.NET2010 <p>Thanks Mike. That's exactly what I wanted and works perfectly.</p> <p></p> <p>Thank you also to the other contributors too</p> <p></p> 2012-05-07T16:46:05-04:00