任何人都可以幫我用公式來計算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