有人可以帮助我计算IPv6地址的十进制值吗?
我需要将公式放在VBA中以制作自定义excel公式。我知道IPv4的公式,我已经读过它类似但我似乎无法弄明白。我需要这样才能将IPv6地址映射到ip2location
CSV。
答案 0 :(得分:0)
我有一些你应该可以开始使用的Excel IP功能。
=SubnetIPv4(IPv4,Bits,Offset) =IsIPv4(IPv4)
IP Address Bits Offset Result Result
10.11.12.13 26 0 10.11.12.0 TRUE
Notes:
Macros must be enabled
IPv4 is a string representing an IPv4 address in dotted decimal format
Bits is an integer (0 to 32) representing the number of mask bits
Offset is an integer representing the host address offset into the subnet
Using Offset 0 will retun a subnet for any IP address
Using IPv4 of 255.255.255.255 and Offset 0 will retun a mask of Bits size
=SubnetIPv6(IPv6,Bits,Offset) =IsIPv6(IPv6)
IP Address Bits Offset Result Result
fe80::1dce:e8b3:1a14:2c3b 10 :: FE80:0:0:0:0:0:0:0 TRUE
Notes:
Macros must be enabled
IPv6 is a string representing an IPv6 address in standard format (leading 0s are optional and :: works)
Bits is an integer (0 to 128) representing the number of mask bits
Offset is a string representing the host address offset into the subnet in standard format (leading 0s are optional and :: works)
Using Offset equivalent to 0 (::, ::0, 0:0:0:0:0:0:0:0, etc.) will retun a subnet for any IP address
Using IPv6 of FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF and Offset equivalent to 0 will retun a mask of Bits size
Function CountStr(Source As String, Target As String) As Integer
Dim c, i As Integer
c = 0
If Not ((Source = "") Or (Target = "")) Then
For i = 1 To Len(Source)
If Mid(Source, i, Len(Target)) = Target Then
c = c + 1
End If
Next
End If
CountStr = c
End Function
Function SubnetIPv4(IPv4 As String, Bits As Integer, Offset As Long) As String
Dim a() As String
Dim c, d, i As Integer
Dim m As Long
Dim s As String
If IPv4 = "" Then
GoTo InvalidIPv4
End If
c = CountStr(IPv4, ".")
If c <> 3 Then
GoTo InvalidIPv4
End If
c = CountStr(IPv4, "..")
If c > 1 Then
GoTo InvalidIPv4
End If
If (Left(IPv4, 1) = ".") Or (Right(IPv4, 1) = ".") Then
GoTo InvalidIPv4
End If
a = Split(IPv4, ".")
If UBound(a) <> 3 Then
GoTo InvalidIPv4
End If
On Error GoTo InvalidIPv4
For i = 0 To 3
If (Len(a(i)) > 0) And (Len(a(i)) < 4) Then
a(i) = CInt(a(i))
If (a(i) < 0) Or (a(i) > 255) Then
GoTo InvalidIPv4
End If
Else
GoTo InvalidIPv4
End If
Next
If (Bits < 0) Or (Bits > 32) Then
GoTo InvalidIPv4
End If
c = Bits Mod 8
d = Bits \ 8
If (Bits <> 0) And (c = 0) Then
c = 8
d = d - 1
End If
m = 0
For i = 0 To 7
m = m * 2
If c > 0 Then
m = m + 1
c = c - 1
End If
Next
a(d) = CStr(CLng(a(d)) And m)
For i = d + 1 To 3
a(i) = "0"
Next
If Offset < 0 Then
GoTo InvalidIPv4
End If
m = 0
For i = 1 To (32 - Bits)
m = m * 2
m = m + 1
Next
If Offset > m Then
GoTo InvalidIPv4
End If
m = Offset
For i = 3 To 0 Step -1
a(i) = a(i) + (m Mod 256)
m = m \ 256
Next
s = ""
For i = 0 To 3
s = s + CStr(a(i)) + "."
Next
s = Left(s, Len(s) - 1)
SubnetIPv4 = s
Exit Function
InvalidIPv4:
Error (3)
End Function
Function IsIPv4(IPv4 As String) As Boolean
Dim s As String
On Error GoTo InvalidIPv4
s = SubnetIPv4(IPv4, 32, 0)
IsIPv4 = True
Exit Function
InvalidIPv4:
IsIPv4 = False
End Function
Function SubnetIPv6(IPv6 As String, Bits As Integer, Offset As String) As String
Dim a() As String
Dim c, d, i As Integer
Dim m As Long
Dim s, t As String
If IPv6 = "" Then
GoTo InvalidIPv6
End If
c = CountStr(IPv6, ":")
If (c < 2) Or (c > 8) Then
GoTo InvalidIPv6
End If
d = CountStr(IPv6, "::")
If d > 1 Then
GoTo InvalidIPv6
End If
If (Left(IPv6, 1) = ":") And (Not (Left(IPv6, 2) = "::")) Then
GoTo InvalidIPv6
End If
If (Right(IPv6, 1) = ":") And (Not (Right(IPv6, 2) = "::")) Then
GoTo InvalidIPv6
End If
s = IPv6
If d = 1 Then
If Left(s, 2) = "::" Then
s = "0" + s
End If
If Right(s, 2) = "::" Then
s = s + "0"
End If
t = ":"
For i = c To 7
t = t + "0:"
Next
s = Replace(s, "::", t)
End If
a = Split(s, ":")
If UBound(a) <> 7 Then
GoTo InvalidIPv6
End If
On Error GoTo InvalidIPv6
For i = 0 To 7
If (Len(a(i)) > 0) And (Len(a(i)) < 5) Then
a(i) = WorksheetFunction.Hex2Dec(a(i))
Else
GoTo InvalidIPv6
End If
Next
If (Bits < 0) Or (Bits > 128) Then
GoTo InvalidIPv6
End If
c = Bits Mod 16
d = Bits \ 16
If (Bits <> 0) And (c = 0) Then
c = 16
d = d - 1
End If
m = 0
For i = 0 To 15
m = m * 2
If c > 0 Then
m = m + 1
c = c - 1
End If
Next
a(d) = CStr(CLng(a(d)) And m)
For i = d + 1 To 7
a(i) = "0"
Next
If Offset = "" Then
GoTo InvalidIPv6
End If
c = CountStr(Offset, ":")
If (c < 2) Or (c > 8) Then
GoTo InvalidIPv6
End If
d = CountStr(Offset, "::")
If d > 1 Then
GoTo InvalidIPv6
End If
If (Left(Offset, 1) = ":") And (Not (Left(Offset, 2) = "::")) Then
GoTo InvalidIPv6
End If
If (Right(Offset, 1) = ":") And (Not (Right(Offset, 2) = "::")) Then
GoTo InvalidIPv6
End If
s = Offset
If d = 1 Then
If Left(s, 2) = "::" Then
s = "0" + s
End If
If Right(s, 2) = "::" Then
s = s + "0"
End If
t = ":"
For i = c To 7
t = t + "0:"
Next
s = Replace(s, "::", t)
End If
b = Split(s, ":")
If UBound(b) <> 7 Then
GoTo InvalidIPv6
End If
On Error GoTo InvalidIPv6
For i = 0 To 7
If (Len(b(i)) > 0) And (Len(b(i)) < 5) Then
b(i) = WorksheetFunction.Hex2Dec(b(i))
Else
GoTo InvalidIPv6
End If
Next
c = Bits Mod 16
d = Bits \ 16
If (Bits <> 0) And (c = 0) Then
c = 16
d = d - 1
End If
m = 0
For i = 0 To 15
m = m * 2
If c > 0 Then
m = m + 1
c = c - 1
End If
Next
For i = 0 To d - 1
If b(i) <> "0" Then
GoTo InvalidIPv6
End If
Next
If b(d) <> CStr(CLng(b(d)) And m) Then
GoTo InvalidIPv6
End If
For i = 7 To d Step -1
a(i) = CStr(CLng(a(i)) + CLng(b(i)))
Next
s = ""
For i = 0 To 7
s = s + WorksheetFunction.Dec2Hex(a(i)) + ":"
Next
s = Left(s, Len(s) - 1)
SubnetIPv6 = s
Exit Function
InvalidIPv6:
Error (3)
End Function
Function IsIPv6(IPv6 As String) As Boolean
Dim s As String
On Error GoTo InvalidIPv6
s = SubnetIPv6(IPv6, 128, "::")
IsIPv6 = True
Exit Function
InvalidIPv6:
IsIPv6 = False
End Function
答案 1 :(得分:0)
IPv6地址的十进制值会很大。您需要128位无符号整数,但VBA中没有此类数据类型。 因此,据我所知,您无法将IPv6地址作为整数进行操作。