Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

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?
  • 3
  • 3
1 Solution
Hello nahumba,

string1.Length > string2.Length

nahumbaAuthor Commented:
Hi martin,
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 ?

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

nahumbaAuthor 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..
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 = ""
        Dim ip2 As String = ""
        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))
        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)
            While (res.Length < 3)
                res = "0" & res

            End While
        End If
        Return res
    End Function
Check this article: http://www.codeguru.com/Csharp/.NET/net_general/internet/article.php/c10651/
this is in c# unfortunately but if you like the style, you can use this convertor for turning the code into vb.net

nahumbaAuthor Commented:
Thank you Martin - exactly what I've been looking for!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now