精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● VB和Basic>>〓〓..技术文章连载..〓〓>>网络相关的编程技巧>>网络相关的编程技巧(4)

主题:网络相关的编程技巧(4)
发信人: plindge()
整理人: fishy(2000-05-23 14:08:10), 站内信件
'=======================================================
' Welcome To Plindge Studio http://plindge.yeah.net
' E-Mail: seeleen@ netease.com
'=======================================================
' (4) Convert Dotted Quad IP Address to Long IP Address and Vice Versa


Public 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

' convert long IP to dotted notation

Public 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" '"Invalid Input" ' whatever
End Function

' Usage Example: Msgbox Dotted2LongIP("202.96.128.68")
'                 Msgbox LongIP2Dotted("3395321924")
' To be continue...

--
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.103.160.26]

[关闭][返回]