java - CIDR for an IPAddress -


i trying find subnet mask ip address, far able quite well. problem cidr potion. got few examples here , there , assembled code.

so far have got:

public class test {  int baseipnumeric; int netmasknumeric;  /**  * specify ip in cidr format like: new ipv4("10.1.0.25/16");  *  *@param ipincidrformat  */ public void doinitialize(string ipincidrformat) throws numberformatexception {      string[] st = ipincidrformat.split("\\/");     if (st.length != 2)          throw new numberformatexception("invalid cidr format '"                 + ipincidrformat + "', should be: xx.xx.xx.xx/xx");      string symbolicip = st[0];     string symboliccidr = st[1];      integer numericcidr = new integer(symboliccidr);     if (numericcidr > 32)          throw new numberformatexception("cidr can not greater 32");      /* ip */     st = symbolicip.split("\\.");      if (st.length != 4)         throw new numberformatexception("invalid ip address: " + symbolicip);      int = 24;     baseipnumeric = 0;      (int n = 0; n < st.length; n++) {          int value = integer.parseint(st[n]);          if (value != (value & 0xff)) {              throw new numberformatexception("invalid ip address: " + symbolicip);         }          baseipnumeric += value << i;         -= 8;      }      /* netmask cidr */     if (numericcidr < 8)         throw new numberformatexception("netmask cidr can not less 8");     netmasknumeric = 0xffffffff;     netmasknumeric = netmasknumeric << (32 - numericcidr);  }    /**  * net mask in symbolic form, i.e. xxx.xxx.xxx.xxx  *  *@return  */  public string getnetmask() {     stringbuffer sb = new stringbuffer(15);      (int shift = 24; shift > 0; shift -= 8) {          // process 3 bytes, high order byte down.         sb.append(integer.tostring((netmasknumeric >>> shift) & 0xff));          sb.append('.');     }     sb.append(integer.tostring(netmasknumeric & 0xff));      return sb.tostring(); }  /**  *@param args  */ public static void main(string[] args) {       test ipv4 = new test();      string ip = "192.168.161.111";     ipv4.doinitialize(ip+"/32");     system.out.println(ipv4.getnetmask());  } 

}

i needing know passing /32 appended ip cidr, 32, if no, how know 1 put in there?


Comments