Převod krátké IP na dlouhou

Funkce:
Private Function Dotted2LongIP(DottedIP As String) As Variant

   On Error Resume Next

   Dim i As Byte, pos As Integer
   Dim PrevPos As Integer, num As Integer

   For i = 1 To 4
      pos = InStr(PrevPos + 1, DottedIP, ".", 1)
      If i = 4 Then pos = Len(DottedIP) + 1
      num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
      PrevPos = pos
      Dotted2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dotted2LongIP
   Next

End Function


Private Function LongIP2Dotted(ByVal LongIP As Variant) As String

   On Error GoTo ExitFun

   If LongIP = "" Or LongIP < 0 Then Err.Raise vbObjectError + 1

   Dim i As Integer, num As Currency

   For i = 1 To 4
      num = Int(LongIP / 256 ^ (4 - i))
      LongIP = LongIP - (num * 256 ^ (4 - i))
      If num > 255 Then Err.Raise vbObjectError + 1
      If i = 1 Then
         LongIP2Dotted = num
      Else
         LongIP2Dotted = LongIP2Dotted & "." & num
      End If
   Next

   Exit Function

ExitFun:
   LongIP2Dotted = "0.0.0.0" 

End Function

Použití:
dotted2longip("10.240.24.38") - vrátí hodnotu 183506982
longip2dotted(183506982)
- vrátí 10.240.128.38

Zpět

Autor: The Bozena