该程序可以将IP转换为
Option Explicit
Public Const OCTET4 As Double = 256# * 256# * 256# * 256#
Public Const OCTET3 As Double = 256# * 256# * 256#
Public Const OCTET2 As Double = 256# * 256#
Public Const OCTET1 As Double = 256#
Function ConvertIPToDecimal(ByVal inpIP As String) As Double
'将IP转换成数字
Dim retValue As Double
Dim ipOctets As Variant, ipComp As Variant
ipComp = Split(inpIP, "/")
If UBound(ipComp) > 0 Then inpIP = ipComp(0)
retValue = 0
ipOctets = Split(inpIP, ".")
If UBound(ipOctets) = 3 Then
retValue = OCTET3 * CDbl(ipOctets(0)) + _
OCTET2 * CDbl(ipOctets(1)) + _
OCTET1 * CDbl(ipOctets(2)) + _
CDbl(ipOctets(3))
End If
ConvertIPToDecimal = retValue
End Function
Function ConvertDecimalToIP(ByVal inpNum As Double) As String
'将数字转换为IP
Dim ipOctets(3) As String
Dim tempOctet As Double
Dim retValue As String
retValue = ""
If inpNum < OCTET4 Then
tempOctet = Int(inpNum / OCTET3)
ipOctets(0) = CStr(tempOctet)
inpNum = inpNum - OCTET3 * tempOctet
tempOctet = Int(inpNum / OCTET2)
ipOctets(1) = CStr(tempOctet)
inpNum = inpNum - OCTET2 * tempOctet
tempOctet = Int(inpNum / OCTET1)
ipOctets(2) = CStr(tempOctet)
inpNum = inpNum - OCTET1 * tempOctet
ipOctets(3) = CStr(Int(inpNum))
retValue = Join(ipOctets, ".")
End If
ConvertDecimalToIP = retValue
End Function
Function ConvertMasktoSubnet(strIP As String, strMask As String) As String
'strIP必须是IP/Subnet
'功能:将IP+Mask形式,转换为IP/Subnet,同时IP为标准的网络地址形式
'返回值:新的IP/subnet形式
Dim strNewIP As String
Dim iSubnet As Integer
strMask = Trim(strMask)
If strMask = "128.0.0.0" Then iSubnet = 1
If strMask = "192.0.0.0" Then iSubnet = 2
If strMask = "224.0.0.0" Then iSubnet = 3
If strMask = "240.0.0.0" Then iSubnet = 4
If strMask = "248.0.0.0" Then iSubnet = 5
If strMask = "252.0.0.0" Then iSubnet = 6
If strMask = "254.0.0.0" Then iSubnet = 7
If strMask = "255.0.0.0" Then iSubnet = 8
If strMask = "255.128.0.0" Then iSubnet = 9
If strMask = "255.192.0.0" Then iSubnet = 10
If strMask = "255.224.0.0" Then iSubnet = 11
If strMask = "255.240.0.0" Then iSubnet = 12
If strMask = "255.248.0.0" Then iSubnet = 13
If strMask = "255.252.0.0" Then iSubnet = 14
If strMask = "255.254.0.0" Then iSubnet = 15
If strMask = "255.255.0.0" Then iSubnet = 16
If strMask = "255.255.128.0" Then iSubnet = 17
If strMask = "255.255.192.0" Then iSubnet = 18
If strMask = "255.255.224.0" Then iSubnet = 19
If strMask = "255.255.240.0" Then iSubnet = 20
If strMask = "255.255.248.0" Then iSubnet = 21
If strMask = "255.255.252.0" Then iSubnet = 22
If strMask = "255.255.254.0" Then iSubnet = 23
If strMask = "255.255.255.0" Then iSubnet = 24
If strMask = "255.255.255.128" Then iSubnet = 25
If strMask = "255.255.255.192" Then iSubnet = 26
If strMask = "255.255.255.224" Then iSubnet = 27
If strMask = "255.255.255.240" Then iSubnet = 28
If strMask = "255.255.255.248" Then iSubnet = 29
If strMask = "255.255.255.252" Then iSubnet = 30
If strMask = "255.255.255.254" Then iSubnet = 31
If strMask = "255.255.255.255" Then iSubnet = 32
strNewIP = Trim(strIP) & "/" & iSubnet
strNewIP = ConvertHostAddrToNetAddr(strNewIP) & "/" & iSubnet
ConvertMasktoSubnet = strNewIP '返回值
End Function
Function ConvertHostAddrToNetAddr(strIPAddr As String) As String
'功能:将strIPAddr中的地址转换为标准网络地址
'返回值:其中的网络地址,掩码个数不返回
'strIPAddr的形式必须是IP/Subnet
Dim varComp As Variant
Dim varMaskNum As Variant
Dim retValue As Variant
Dim dIP As Double
varComp = Split(Trim(strIPAddr), "/")
varMaskNum = CInt(varComp(1)) '掩码位
dIP = ConvertIPToDecimal(strIPAddr)
dIP = Int(dIP / (2 ^ (32 - varMaskNum))) '根据掩码位数,即网络地址位数,取响应的IP位数 32-varMaskNum就是主机位数
dIP = dIP * (2 ^ (32 - varMaskNum)) '根据原则,主机位数全0,所以左移主机位数
retValue = ConvertDecimalToIP(dIP)
ConvertHostAddrToNetAddr = retValue
End Function
Function ConvertIPToStandard(strIP As String) As String
'功能:将IP/subnet中的IP地址转换为标准的网络地址形式IP/subnet
Dim varComp As Variant
Dim varSubnet As Variant
Dim retValue As Variant
varComp = Split(Trim(strIP), "/")
varSubnet = CInt(varComp(1))
retValue = ConvertHostAddrToNetAddr(strIP) & "/" & varSubnet
ConvertIPToStandard = retValue
End Function
Function GetNumOfIP(strIP As String) As String
'功能:计算IP地址的数量(具体的IP)
'注意:IPAddr必须是IP/subnet形式
Dim retValue As Variant
Dim varSubnet As Variant
Dim varComp As Variant
varComp = Split(Trim(strIP), "/")
varSubnet = CInt(varComp(1))
retValue = 2 ^ (32 - varSubnet) - 2
GetNumOfIP = retValue
End Function
Function GetNumOfIP2(strIP As String) As String
'功能:计算IP地址的数量(C类地址的数量)
'注意:strIP必须是IP/Subnet形式
Dim retValue As Variant
Dim varSubnet As Variant
Dim varComp As Variant
varComp = Split(Trim(strIP), "/")
varSubnet = CInt(varComp(1))
retValue = CInt(2 ^ (32 - varSubnet) / 256)
GetNumOfIP2 = retValue
End Function
Function GetStartIP(strIP As String) As String
'注意:IPAddr必须是IP/subnet形式
Dim retValue As Variant
Dim varStartIP As Variant
Dim varTemp As Variant
Dim varTemp2 As Variant
Dim varIP As Variant
'varTemp = ConvertIPToStandard(strIP)
varTemp = Split(Trim(ConvertIPToStandard(strIP)), "/")
varIP = varTemp(0)
varIP = ConvertIPToDecimal(varIP)
varStartIP = ConvertDecimalToIP(varIP + 1)
retValue = varStartIP
GetStartIP = retValue
End Function
Function GetEndIP(strIP As String) As String
'注意:IPAddr必须是IP/subnet形式
Dim retValue As Variant
Dim varEndIP As Variant
Dim varTemp As Variant
Dim varIP As Variant
Dim varSubnet As Variant
Dim a As Variant
'varTemp = ConvertIPToStandard(strIP)
varTemp = Split(Trim(ConvertIPToStandard(strIP)), "/")
varIP = varTemp(0) 'IP
varSubnet = CInt(varTemp(1)) '掩码个数
varIP = ConvertIPToDecimal(varIP)
varEndIP = ConvertDecimalToIP(varIP + 2 ^ (32 - varSubnet) - 2)
retValue = varEndIP
GetEndIP = retValue
End Function
Function Test() As String
Dim strIP As String
Dim temp As Variant
Dim temp2 As Variant
Dim retValue As String
strIP = "115.62.122.3/21"
Debug.Print "个数:" & GetNumOfIP(strIP)
Debug.Print "起始IP:" & GetStartIP(strIP)
Debug.Print "结束IP:" & GetEndIP(strIP)
Debug.Print ConvertIPToStandard(strIP)
Debug.Print ConvertMasktoSubnet("192.168.1.1", "255.255.255.0")
Debug.Print GetNumOfIP2("192.168.1.1/22")
End Function