Converting CIDR address to subnet mask and network address

Dmitry picture Dmitry · May 31, 2010 · Viewed 67.6k times · Source

Given a CIDR address, e.g. 192.168.10.0/24

  • How to determine mask length? (24)
  • How to determine mask address? (255.255.255.0)
  • How to determine network address? (192.168.10.0)

Answer

Yuriy picture Yuriy · Jul 28, 2011

It is covered by apache utils.

See this URL: http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html

String subnet = "192.168.0.3/31";
SubnetUtils utils = new SubnetUtils(subnet);

utils.getInfo().isInRange(address)

Note: For use w/ /32 CIDR subnets, for exemple, one needs to add the following declaration :

utils.setInclusiveHostCount(true);