VBA IP相关的转换程序

来源:互联网 发布:基于51单片机的交通灯 编辑:程序博客网 时间:2024/04/28 17:26

该程序可以将IP转换为

Option ExplicitPublic 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 = retValueEnd FunctionFunction 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 = retValueEnd FunctionFunction ConvertMasktoSubnet(strIP As String, strMask As String) As String'strIP必须是IP/Subnet'功能:将IP+Mask形式,转换为IP/Subnet,同时IP为标准的网络地址形式'返回值:新的IP/subnet形式Dim strNewIP As StringDim iSubnet As IntegerstrMask = Trim(strMask)If strMask = "128.0.0.0" Then iSubnet = 1If strMask = "192.0.0.0" Then iSubnet = 2If strMask = "224.0.0.0" Then iSubnet = 3If strMask = "240.0.0.0" Then iSubnet = 4If strMask = "248.0.0.0" Then iSubnet = 5If strMask = "252.0.0.0" Then iSubnet = 6If strMask = "254.0.0.0" Then iSubnet = 7If strMask = "255.0.0.0" Then iSubnet = 8If strMask = "255.128.0.0" Then iSubnet = 9If strMask = "255.192.0.0" Then iSubnet = 10If strMask = "255.224.0.0" Then iSubnet = 11If strMask = "255.240.0.0" Then iSubnet = 12If strMask = "255.248.0.0" Then iSubnet = 13If strMask = "255.252.0.0" Then iSubnet = 14If strMask = "255.254.0.0" Then iSubnet = 15If strMask = "255.255.0.0" Then iSubnet = 16If strMask = "255.255.128.0" Then iSubnet = 17If strMask = "255.255.192.0" Then iSubnet = 18If strMask = "255.255.224.0" Then iSubnet = 19If strMask = "255.255.240.0" Then iSubnet = 20If strMask = "255.255.248.0" Then iSubnet = 21If strMask = "255.255.252.0" Then iSubnet = 22If strMask = "255.255.254.0" Then iSubnet = 23If strMask = "255.255.255.0" Then iSubnet = 24If strMask = "255.255.255.128" Then iSubnet = 25If strMask = "255.255.255.192" Then iSubnet = 26If strMask = "255.255.255.224" Then iSubnet = 27If strMask = "255.255.255.240" Then iSubnet = 28If strMask = "255.255.255.248" Then iSubnet = 29If strMask = "255.255.255.252" Then iSubnet = 30If strMask = "255.255.255.254" Then iSubnet = 31If strMask = "255.255.255.255" Then iSubnet = 32strNewIP = Trim(strIP) & "/" & iSubnetstrNewIP = ConvertHostAddrToNetAddr(strNewIP) & "/" & iSubnetConvertMasktoSubnet = strNewIP '返回值End FunctionFunction 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 = retValueEnd FunctionFunction ConvertIPToStandard(strIP As String) As String'功能:将IP/subnet中的IP地址转换为标准的网络地址形式IP/subnetDim varComp As VariantDim varSubnet As VariantDim retValue As VariantvarComp = Split(Trim(strIP), "/")varSubnet = CInt(varComp(1))retValue = ConvertHostAddrToNetAddr(strIP) & "/" & varSubnetConvertIPToStandard = retValueEnd FunctionFunction GetNumOfIP(strIP As String) As String'功能:计算IP地址的数量(具体的IP)'注意:IPAddr必须是IP/subnet形式Dim retValue As VariantDim varSubnet As VariantDim varComp As VariantvarComp = Split(Trim(strIP), "/")varSubnet = CInt(varComp(1))retValue = 2 ^ (32 - varSubnet) - 2GetNumOfIP = retValueEnd FunctionFunction GetNumOfIP2(strIP As String) As String'功能:计算IP地址的数量(C类地址的数量)'注意:strIP必须是IP/Subnet形式Dim retValue As VariantDim varSubnet As VariantDim varComp As VariantvarComp = Split(Trim(strIP), "/")varSubnet = CInt(varComp(1))retValue = CInt(2 ^ (32 - varSubnet) / 256)GetNumOfIP2 = retValueEnd FunctionFunction GetStartIP(strIP As String) As String'注意:IPAddr必须是IP/subnet形式Dim retValue As VariantDim varStartIP As VariantDim varTemp As VariantDim varTemp2 As VariantDim varIP As Variant'varTemp = ConvertIPToStandard(strIP)varTemp = Split(Trim(ConvertIPToStandard(strIP)), "/")varIP = varTemp(0)varIP = ConvertIPToDecimal(varIP)varStartIP = ConvertDecimalToIP(varIP + 1)retValue = varStartIPGetStartIP = retValueEnd FunctionFunction GetEndIP(strIP As String) As String'注意:IPAddr必须是IP/subnet形式Dim retValue As VariantDim varEndIP As VariantDim varTemp As VariantDim varIP As VariantDim varSubnet As VariantDim a As Variant'varTemp = ConvertIPToStandard(strIP)varTemp = Split(Trim(ConvertIPToStandard(strIP)), "/")varIP = varTemp(0) 'IPvarSubnet = CInt(varTemp(1)) '掩码个数varIP = ConvertIPToDecimal(varIP)varEndIP = ConvertDecimalToIP(varIP + 2 ^ (32 - varSubnet) - 2)retValue = varEndIPGetEndIP = retValueEnd FunctionFunction Test() As StringDim strIP As StringDim temp As VariantDim temp2 As VariantDim retValue As StringstrIP = "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







0 0
原创粉丝点击