## Calculate Subnet Mask Function

Posted On 2012-09-16 by dwirch
Keywords: VB6 Visual Basic 6 IP Subnet Newtwork networking function
Tags: Visual Basic 6 Tutorial Tip Windows
Views: 909

Most developers don't have much of a clue about network fundamentals. What may be a simple thing for a network engineer or system administrator may leave a developer simply scratching his or her head.

This function can help those developers out there that need to figure out what to put into the subnet mask field of their network configuration.

Insert the first and last IP address of your range and it will calculate the subnet mask that fits these values with the starting and ending address for this subnet.

Usage:
`Dim MySubnet as String MySubnet = CalculateSubnetMask("192.168.91.254","192.168.90.0") `
And here is the code for the function. Simply paste this into a module, and you should be good to go.
`Function CalculateSubnetMask(HighIP As String, LowIP As String) As String'Insert the first and last IP address of your range and it will calculate the subnet mask'that fits these values with the starting and ending address for this subnet.'get the high numbersIf UBound(Split(HighIP, ".")) = 3 Then PrevPos = 0 For x = 1 To 4 Pos = InStr(PrevPos + 1, HighIP, ".", 1) If x = 4 Then Pos = Len(HighIP) + 1 Num = Int(Mid(HighIP, PrevPos + 1, Pos - PrevPos - 1)) PrevPos = Pos Select Case x Case 1 highnumber4 = Num Case 2 highnumber3 = Num Case 3 highnumber2 = Num Case 4 highnumber1 = Num End Select NextEnd If'get the Low numbersIf UBound(Split(LowIP, ".")) = 3 Then PrevPos = 0 For x = 1 To 4 Pos = InStr(PrevPos + 1, LowIP, ".", 1) If x = 4 Then Pos = Len(LowIP) + 1 Num = Int(Mid(LowIP, PrevPos + 1, Pos - PrevPos - 1)) PrevPos = Pos Select Case x Case 1 lownumber4 = Num Case 2 lownumber3 = Num Case 3 lownumber2 = Num Case 4 lownumber1 = Num End Select NextEnd If'Set up result variables.Dim subnetmask4 As IntegerDim subnetmask3 As IntegerDim subnetmask2 As IntegerDim subnetmask1 As Integer'Figure out the higest bit that changes.'We do this by first doing a binary xor of the low and high numbers.subnetmask4 = highnumber4 Xor lownumber4subnetmask3 = highnumber3 Xor lownumber3subnetmask2 = highnumber2 Xor lownumber2subnetmask1 = highnumber1 Xor lownumber1'Then we round it up to the next most significant digit.subnetmask4 = roundup(subnetmask4)subnetmask3 = roundup(subnetmask3)subnetmask2 = roundup(subnetmask2)subnetmask1 = roundup(subnetmask1)'Figure out which set of numbers changes, then set the lower numbers from it to 0.If (subnetmask4 < 255) Thensubnetmask3 = 0subnetmask2 = 0subnetmask1 = 0End IfIf (subnetmask3 < 255) Thensubnetmask2 = 0subnetmask1 = 0End IfIf (subnetmask2 < 255) Then subnetmask1 = 0CalculateSubnetMask = subnetmask4 & "." & subnetmask3 & "." & subnetmask2 & "." & subnetmask1End FunctionFunction roundup(subnetmaskx As Integer) As Integer 'We need to figure out the most significant bit, then set the subnetmaskx to that number. Select Case subnetmaskx Case Is = 0 roundup = 255 Case Is < 2 roundup = 254 Case Is < 4 roundup = 252 Case Is < 8 roundup = 248 Case Is < 16 roundup = 240 Case Is < 32 roundup = 224 Case Is < 64 roundup = 192 Case Is < 128 roundup = 128 Case Is < 256 roundup = 0 Case Else roundup = 0 End SelectEnd Function`

### About the Author

dwirch has posted a total of 172 articles.

## Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Before you can post, you need to prove you are human. If you log in, this test goes away.

Beat With A Stick List Returns: By request, BWASL has returned to the site. Free your rage by telling the world about something that really grinds your gears. Add your beating.

### Recent Forum Posts

BWASL returns
dwirch posted on May 13, 2017 at about 15:24 in Site News

BWASL returns
dwirch posted on May 13, 2017 at about 8:46 in Site News

Job Spammer: Balashankar Bose Bose
dwirch posted on May 11, 2017 at about 10:05 in Spammers

Job Spammer: Bharti Jigyasi
dwirch posted on May 11, 2017 at about 7:58 in Spammers

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers