任何人都可以帮我用公式来计算IPv6地址的十进制值吗?获取ipv6地址的十进制值
我需要把公式放在VBA中来制作一个自定义的Excel公式。我知道IPv4的公式,我已经读过它,但似乎无法弄清楚。我需要这样才能将IPv6地址映射到ip2location
CSV中的范围。
任何人都可以帮我用公式来计算IPv6地址的十进制值吗?获取ipv6地址的十进制值
我需要把公式放在VBA中来制作一个自定义的Excel公式。我知道IPv4的公式,我已经读过它,但似乎无法弄清楚。我需要这样才能将IPv6地址映射到ip2location
CSV中的范围。
我有一些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
您还没有解释这些函数的功能以及它如何解决OP将IPv6字符串表示为十进制值的问题。 – lurker 2014-11-05 17:50:09
我不知道我能解释什么。这些功能是我如何得到它们的。 OP表达了创建自定义Excel公式的愿望。每个IPv4和IPv6都有两个功能来验证和计算地址。我发现它们很有用,而OP可能能够对代码做些什么。 – 2014-11-05 18:12:38
我无法真正解释一切。我添加了一些内容,显示了我如何使用这些内容。 – 2014-11-05 18:37:01
IPv6地址的十进制值将会很大。您需要一个128位无符号整数,但在VBA中没有这种数据类型。 所以据我所知,你不能把IPv6地址作为整数来处理。
那么你可以从IPv6地址是8位16位值这一事实中找出答案。所以你可以用65536(2^16)的幂来计算多项式。这将是一个伟大的'honkin'号码。如果你需要的不仅仅是谷歌的“IPv6到十进制转换”。很多点击。 – lurker 2014-11-05 15:54:53
它确实就像是IPv4的情况,但是对于IPv6,您拥有65536而不是255的权力,并且您有8项而不是4。 – lurker 2014-11-05 16:02:10
好吧,但是当我使用计算器并与ip2location csv比较时,我会得到正确的结果。如果我将ipv4公式更改为8个术语并乘以65536而不是256的幂,我会得到完全不同的数字,然后计算器或csv会给我什么。所以我犯了一个错误的地方,但我不知道在哪里.. – 2014-11-05 16:06:06