How do I get generate an IP address range given start and end IP address?

后端 未结 5 448
既然无缘
既然无缘 2020-12-19 06:12

How can generate a range of IP addresses from a start and end IP address?

Example for a network \"192.168.0.0/24\":

String start = \"192.168.0.2\"
S         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 06:37

    Recognize that each of the 4 components of an IPv4 address is really a hex number between 00 and FF.

    If you change your start and end IP addresses into 32 bit unsigned integers, you can just loop from the lowest one to the highest one and convert each value you loop through back into the IP address format.

    The range in the example you give is C0A80002 to C0A800FE.

    Here's a link to code that converts between a hex number and an IPv4 address

    http://technojeeves.com/joomla/index.php/free/58-convert-ip-address-to-number

提交回复
热议问题