Java和IP - 子网解析

时间:2013-11-13 04:45:13

标签: java networking subnet

java中有一种简单的方法可以从

的格式获取IP的范围

175.25.0.0/16

有点像 175.25.0.0-175.25.255.255

1 个答案:

答案 0 :(得分:0)

  

public String getHostAddressRange(String netmaskNumeric){

    int numberOfBits;
    for (numberOfBits = 0; numberOfBits < 32; numberOfBits++) {

        if ((netmaskNumeric << numberOfBits) == 0)
            break;
    }
    Integer numberOfIPs = 0;
    for (int n = 0; n < (32 - numberOfBits); n++) {

        numberOfIPs = numberOfIPs << 1;
        numberOfIPs = numberOfIPs | 0x01;

    }

    Integer baseIP = baseIPnumeric & netmaskNumeric;
    String firstIP = convertNumericIpToSymbolic(baseIP + 1);
    String lastIP = convertNumericIpToSymbolic(baseIP + numberOfIPs - 1);
    return firstIP + " - " + lastIP;
}