Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

java - Converting CIDR address to subnet mask and network address

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)
question from:https://stackoverflow.com/questions/2942299/converting-cidr-address-to-subnet-mask-and-network-address

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...