• Status: Solved
• Priority: Medium
• Security: Public
• Views: 246

# IP address as string - determine if it is longer or short then the other - vb.net

Hi all,

I have two strings which represents an IP address.
I think it isn't possible, but I wanted to know, if there's a way to know if one string's length is longer / short then the other?
0
nahumba
• 3
• 3
1 Solution

Commented:
Hello nahumba,

string1.Length > string2.Length

Regards,
Martin
0

Author Commented:
Hi martin,
This will not always work (I think), since the actual length does not always represents the IP address lentgh...
0

Commented:
what do you mean ?
can you give some examples so I can be more strict in suggestions ?

Martin
0

Author Commented:
My application needs to scan a range of IPs from a given start address to an end address.

I need to determine that the start address's length is shorter.. What I actually mean is, that the IP address length is determined by it's byte size (each octet is between 0 to 255) 32bits in total...

So the string's length value just return the number of total characters in the string, I need to determine the IP address's byte size..
0

Commented:
You can do something like this

'Assumption IP is in valid IPV4 format
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ip1 As String = "255.255.255.255"
Dim ip2 As String = "2.2.2.2"
Dim lngip1 As Long = getIPAsLong(ip1)
Dim lngip2 As Long = getIPAsLong(ip2)

If lngip1 > lngip2 Then
MessageBox.Show("ip1 is larger then ip2")
ElseIf lngip1 = lngip2 Then
MessageBox.Show("ip1 is equal to  ip2")
Else 'lngip1 < lngip2
MessageBox.Show("ip1 is smaller than  ip2")
End If
End Sub

Private Function getIPAsLong(ByVal str As String) As Long
Dim sRes As String
Dim i As Integer
Dim aStr() As String
aStr = str.Split(".")
For i = 0 To aStr.Length - 1
Next
Return CLng(sRes)

End Function

Private Function paddingZero(ByVal str As String) As String
Dim res As String = str.Trim
If (res.Length >= 3) Then
res = res.Substring(0, 3)
Else
While (res.Length < 3)
res = "0" & res

End While
End If
Return res
End Function
0

Commented:
this is in c# unfortunately but if you like the style, you can use this convertor for turning the code into vb.net
http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

HTH
Martin
0

Author Commented:
Thank you Martin - exactly what I've been looking for!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.