How can I replicate
this in VB.NET 2010 in Framework 4?
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
No Google links please as they will be ignored. If all you do is post links them please ruin another thread
Thanks in advance
subnet
Please click 'Mark as Answer' if my reply helped
Homepage | YouTube Channel | Post code, not links
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]() {"/"C})
ip = IP2Int(elements(0))
bits = Convert.ToInt32(elements(1))
mask = Not (&HffffffffUI >> bits)
network = ip And mask
broadcast = network + Not mask
usableIps = If((bits > 30), 0, (broadcast - network - 1))
If usableIps <= 0 Then
startIP = InlineAssignHelper(endIP, 0)
Else
startIP = network + 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]() {"."C})
If elements.Length = 4 Then
ip = Convert.ToUInt32(elements(0)) << 24
ip += Convert.ToUInt32(elements(1)) << 16
ip += Convert.ToUInt32(elements(2)) << 8
ip += Convert.ToUInt32(elements(3))
End If
Return ip
End Function
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++ in Visual Studio 98) I have made an error
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)
Any ideas on how I can achieve my original goal?
Please click 'Mark as Answer' if my reply helped
Homepage | YouTube Channel | Post code, not links
Sorry I posted from related code, but not the correct one.
Try this one, it takes input 192.168.0.1/25 and outputs 192.168.0.1 - 192.168.0.126
IPAddress ip = new IPAddress(new byte[] { 192, 168, 0, 1 });
int bits = 25;
uint mask = ~(uint.MaxValue >> 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 < ipBytes.Length; i++)
{
startIPBytes[i] = (byte)(ipBytes[i] & 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);
public static IPAddress CreateByHostBitLength(int hostpartLength)
{
int hostPartLength = hostpartLength;
int netPartLength = 32 - hostPartLength;
if (netPartLength < 2)
throw new ArgumentException("Number of hosts is to large for IPv4");
Byte[] binaryMask = new byte[4];
for (int i = 0; i < 4; i++)
{
if (i * 8 + 8 <= netPartLength)
binaryMask[i] = (byte)255;
else if (i * 8 > netPartLength)
binaryMask[i] = (byte)0;
else {
int oneLength = netPartLength - i * 8;
string binaryDigit =
String.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
binaryMask[i] = Convert.ToByte(binaryDigit, 2);
}
}
return new IPAddress(binaryMask);
}
public static IPAddress CreateByNetBitLength(int netpartLength)
{
int hostPartLength = 32 - netpartLength;
return CreateByHostBitLength(hostPartLength);
}
public static IPAddress CreateByHostNumber(int numberOfHosts)
{
int maxNumber = numberOfHosts + 1;
string b = Convert.ToString(maxNumber, 2);
return CreateByHostBitLength(b.Length);
}
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 + 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
Please click 'Mark as Answer' if my reply helped
Homepage | YouTube Channel | Post code, not links
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
Please click 'Mark as Answer' if my reply helped
Homepage | YouTube Channel | Post code, not links
Public Function ConvertCIDRToMask(cidr As Integer) As String
Dim ip As Int64 = Convert.ToInt64(New String("1"c, cidr).PadRight(32, "0"c), 2)
Return (String.Join(".", New Net.IPAddress(ip).GetAddressBytes.Reverse))
End Function
Mike Banavige
Marked as answer by crouchie2004 on May 07, 2012 04:46 PM
crouchie2004
Member
497 Points
306 Posts
CIDR to Subnet in VB.NET2010
May 03, 2012 06:28 PM|LINK
How can I replicate this in VB.NET 2010 in Framework 4?
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
No Google links please as they will be ignored. If all you do is post links them please ruin another thread
Thanks in advance
subnet
Homepage | YouTube Channel | Post code,
not linkswikkard
Member
204 Points
38 Posts
Re: CIDR to Subnet in VB.NET2010
May 03, 2012 10:42 PM|LINK
Try this :
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]() {"/"C}) ip = IP2Int(elements(0)) bits = Convert.ToInt32(elements(1)) mask = Not (&HffffffffUI >> bits) network = ip And mask broadcast = network + Not mask usableIps = If((bits > 30), 0, (broadcast - network - 1)) If usableIps <= 0 Then startIP = InlineAssignHelper(endIP, 0) Else startIP = network + 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]() {"."C}) If elements.Length = 4 Then ip = Convert.ToUInt32(elements(0)) << 24 ip += Convert.ToUInt32(elements(1)) << 16 ip += Convert.ToUInt32(elements(2)) << 8 ip += Convert.ToUInt32(elements(3)) End If Return ip End Functionwww.icle.com.au
www.wikkard.net
crouchie2004
Member
497 Points
306 Posts
Re: CIDR to Subnet in VB.NET2010
May 04, 2012 10:30 AM|LINK
Hi,
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++ in Visual Studio 98) I have made an error
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)
Any ideas on how I can achieve my original goal?
Homepage | YouTube Channel | Post code,
not linkswikkard
Member
204 Points
38 Posts
Re: CIDR to Subnet in VB.NET2010
May 07, 2012 12:00 AM|LINK
Sorry I posted from related code, but not the correct one.
Try this one, it takes input 192.168.0.1/25 and outputs 192.168.0.1 - 192.168.0.126
www.icle.com.au
www.wikkard.net
wikkard
Member
204 Points
38 Posts
Re: CIDR to Subnet in VB.NET2010
May 07, 2012 12:05 AM|LINK
Sorry whilst the above is again very close you what you want it isn't exactly what you asked for.
I believe that the below will do what you want. (shamelessly lifted from http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx)
www.icle.com.au
www.wikkard.net
crouchie2004
Member
497 Points
306 Posts
Re: CIDR to Subnet in VB.NET2010
May 07, 2012 12:08 AM|LINK
Homepage | YouTube Channel | Post code,
not linkscrouchie2004
Member
497 Points
306 Posts
Re: CIDR to Subnet in VB.NET2010
May 07, 2012 12:11 AM|LINK
Homepage | YouTube Channel | Post code,
not linksmbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: CIDR to Subnet in VB.NET2010
May 07, 2012 01:15 AM|LINK
something like this:
Public Function ConvertCIDRToMask(cidr As Integer) As String Dim ip As Int64 = Convert.ToInt64(New String("1"c, cidr).PadRight(32, "0"c), 2) Return (String.Join(".", New Net.IPAddress(ip).GetAddressBytes.Reverse)) End Functioncrouchie2004
Member
497 Points
306 Posts
Re: CIDR to Subnet in VB.NET2010
May 07, 2012 04:46 PM|LINK
Thanks Mike. That's exactly what I wanted and works perfectly.
Thank you also to the other contributors too
Homepage | YouTube Channel | Post code,
not links