nahumba
asked on
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?
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?
ASKER
Hi martin,
This will not always work (I think), since the actual length does not always represents the IP address lentgh...
This will not always work (I think), since the actual length does not always represents the IP address lentgh...
what do you mean ?
can you give some examples so I can be more strict in suggestions ?
Martin
can you give some examples so I can be more strict in suggestions ?
Martin
ASKER
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..
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..
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
sRes &= paddingZero(aStr(i))
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
'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
sRes &= paddingZero(aStr(i))
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Martin - exactly what I've been looking for!
string1.Length > string2.Length
Regards,
Martin