VBA轉換IP地址為可比較12位字串(去掉小數點)

pds911發表於2008-09-04

[原創]轉載請註明出處,-THX-

完成功能樣例:zhip("10.12.3.251") ->010012003251

Function zhip(ip As String) As String
Dim pos, StartPos, NumChars As Integer
Dim ip_val, ret_str As String

ip_val = ""
ret_str = ""
StartPos = 1
pos = 0
For i = 1 To 4
pos = InStr(StartPos, ip, ".")
If pos > 0 Then
NumChars = pos - StartPos
If Len(NumChars) = 2 Then
ip_val = "0" & Mid(ip, StartPos, NumChars)
ElseIf Len(NumChars) = 1 Then
ip_val = "00" & Mid(ip, StartPos, NumChars)
Else
ip_val = Mid(ip, StartPos, NumChars)
End If
StartPos = pos + 1
ret_str = ret_str & ip_val
Else
If Len(Mid(ip, StartPos)) = 1 Then
ret_str = ret_str & "00" & Mid(ip, StartPos)
End If
If Len(Mid(ip, StartPos)) = 2 Then
ret_str = ret_str & "0" & Mid(ip, StartPos)
End If
If Len(Mid(ip, StartPos)) = 3 Then
ret_str = ret_str & Mid(ip, StartPos)
End If
End If
Next
zhip = ret_str
End Function

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/81211/viewspace-1010116/,如需轉載,請註明出處,否則將追究法律責任。

相關文章