Function IP2Long()

This function converts a string containing an IP address in dot notation into a number.

It can be used in Calc to convert IP addresses into numeric values and then sort the resulting column.

The function is called IP2Long() but really returns a Double. This is due to the fact the there is no unsigned long integer in StarOffice Basic.


REM  *****  BASIC  *****

Function IP2Long(IPString) as Double

  DIM res as Double

  GlobalScope.BasicLibraries.LoadLibrary("Tools")

  AddrComponents = ArrayOutOfString(IPString, ".")
  
  IF UBound(AddrComponents) = 3 THEN
    
    res = 16777216 * Val(AddrComponents(0))
    res = res + 65536 * Val(AddrComponents(1))
    res = res + 256 * Val(AddrComponents(2))
    res = res + Val(AddrComponents(3))
    
  ELSE
  
    res = 0
    
  END IF

  IP2Long = res

End Function

Function Main(IPString as Variant) as Double

  DIM res as Double
    
  IF IsMissing(IPString) THEN
  
    res = 0
    
  ELSEIF IsArray(IPString) THEN
  
    res = 0
    
  ELSEIF IsNumeric(IPString) THEN
  
    res = IP2Long(Format(IPString, "#"))
    
  ELSE

    res = IP2Long(IPString)
    
  END IF
  
  Main = res

End Function

I place this code into the public domain.

AttachmentSize
ip2long.txt693 bytes