算ip这样 Private Sub Command1_Click() Dim str1() As String Dim str2() As String Dim str3() As String Dim strAnd(3) As String str1 = Split(Text1.Text, ".") str2 = Split(Text2.Text, ".") str3 = Split(Text3.Text, ".")
For i = 0 To 3 strAnd(i) = str1(i) And str2(i) strAnd(i) = strAnd(i) And str3(i) Next i
Debug.Print Join(strAnd, ".") End Sub
Option ExplicitSub Main() Dim aIP(3) As String Dim aBin(3) As String Dim i As Long Dim j As Long
For i = 0 To 3 aBin(i) = IP2Bin(aIP(i)) Debug.Print aIP(i), aBin(i), Bin2IP(aBin(i)) Next
For j = 1 To Len(aBin(0)) For i = 1 To 3 If Mid$(aBin(i), j, 1) <> Mid$(aBin(0), j, 1) Then GoTo FoundDiff Next Next FoundDiff:
Debug.Print Bin2IP(Left$(aBin(i), j - 1)) End SubPublic Function IP2Bin(ByVal IP As String) As String Dim a() As String Dim lBits As Long Dim s As String Dim i As Long
a = Split(IP & "/24", "/") lBits = a(1)
a = Split(a(0), ".")
For i = 0 To 3 s = s & Byte2Bin(CByte(a(i))) Next
IP2Bin = Left$(s, lBits) End FunctionPublic Function Bin2IP(ByVal Bin As String) As String Dim lBits As Long Dim s As String Dim i As Long
lBits = Len(Bin) If lBits < 24 Then Bin = Bin & String(24 - lBits, "0") End If
For i = 0 To 3 s = s & IIf(i > 0, ".", vbNullString) & Bin2Byte(Mid$(Bin, i * 8 + 1, 8)) Next
Bin2IP = s & "/" & lBits End FunctionPublic Function Byte2Bin(ByVal n As Byte) As String Dim s As String Dim nMask As Byte Dim i As Long
s = String$(8, "0") nMask = &H80 For i = 1 To 8 If (n And nMask) <> 0 Then Mid$(s, i, 1) = "1" End If
nMask = nMask \ 2 Next
Byte2Bin = s End FunctionPublic Function Bin2Byte(ByVal Bin As String) As Byte Dim n As Byte Dim nMask As Byte Dim i As Long
nMask = &H80 For i = 1 To 8 If Mid$(Bin, i, 1) = "1" Then n = n Or nMask End If
Dim str1() As String
Dim str2() As String
Dim str3() As String
Dim strAnd(3) As String str1 = Split(Text1.Text, ".")
str2 = Split(Text2.Text, ".")
str3 = Split(Text3.Text, ".")
For i = 0 To 3
strAnd(i) = str1(i) And str2(i)
strAnd(i) = strAnd(i) And str3(i)
Next i
Debug.Print Join(strAnd, ".")
End Sub
Dim aIP(3) As String
Dim aBin(3) As String
Dim i As Long
Dim j As Long
aIP(0) = "172.168.4.1/24"
aIP(1) = "172.168.5.1/24"
aIP(2) = "172.168.6.1/24"
aIP(3) = "172.168.7.1/24"
For i = 0 To 3
aBin(i) = IP2Bin(aIP(i))
Debug.Print aIP(i), aBin(i), Bin2IP(aBin(i))
Next
For j = 1 To Len(aBin(0))
For i = 1 To 3
If Mid$(aBin(i), j, 1) <> Mid$(aBin(0), j, 1) Then GoTo FoundDiff
Next
Next
FoundDiff:
Debug.Print Bin2IP(Left$(aBin(i), j - 1))
End SubPublic Function IP2Bin(ByVal IP As String) As String
Dim a() As String
Dim lBits As Long
Dim s As String
Dim i As Long
a = Split(IP & "/24", "/")
lBits = a(1)
a = Split(a(0), ".")
For i = 0 To 3
s = s & Byte2Bin(CByte(a(i)))
Next
IP2Bin = Left$(s, lBits)
End FunctionPublic Function Bin2IP(ByVal Bin As String) As String
Dim lBits As Long
Dim s As String
Dim i As Long
lBits = Len(Bin)
If lBits < 24 Then
Bin = Bin & String(24 - lBits, "0")
End If
For i = 0 To 3
s = s & IIf(i > 0, ".", vbNullString) & Bin2Byte(Mid$(Bin, i * 8 + 1, 8))
Next
Bin2IP = s & "/" & lBits
End FunctionPublic Function Byte2Bin(ByVal n As Byte) As String
Dim s As String
Dim nMask As Byte
Dim i As Long
s = String$(8, "0")
nMask = &H80
For i = 1 To 8
If (n And nMask) <> 0 Then
Mid$(s, i, 1) = "1"
End If
nMask = nMask \ 2
Next
Byte2Bin = s
End FunctionPublic Function Bin2Byte(ByVal Bin As String) As Byte
Dim n As Byte
Dim nMask As Byte
Dim i As Long
nMask = &H80
For i = 1 To 8
If Mid$(Bin, i, 1) = "1" Then
n = n Or nMask
End If
nMask = nMask \ 2
Next
Bin2Byte = n
End Function