seancfoley / IPAddress

Java library for handling IP addresses and subnets, both IPv4 and IPv6
https://seancfoley.github.io/IPAddress/
Apache License 2.0
466 stars 63 forks source link

bug? #116

Closed YooLin closed 10 months ago

YooLin commented 10 months ago
        IPAddressString ipAddressString = new IPAddressString("157.0.50.55/11");
        IPAddressSeqRange ipAddressRange = ipAddressString.getSequentialRange();
        IPAddress lower = ipAddressRange.getLower();
        IPAddress upper = ipAddressRange.getUpper();
        System.out.println("lower = " + lower);
        System.out.println("upper = " + upper);

print

lower = 157.0.50.55
upper = 157.0.50.55

expect

lower=157.0.0.0 
upper=157.31.255.255

Is this a bug?

YooLin commented 10 months ago
        <dependency>
            <groupId>com.github.seancfoley</groupId>
            <artifactId>ipaddress</artifactId>
            <version>5.4.0</version>
        </dependency>
YooLin commented 10 months ago

after read #114 .Using the following method got me what I wanted.

        String ip = "157.0.50.55/11";
        IPAddress ipAddress = new IPAddressString(ip).getAddress().toPrefixBlock();
        System.out.println("lower = " + ipAddress.getLower().toSubnetString());
        System.out.println("upper = " + ipAddress.getUpper().toSubnetString());

print

lower = 157.0.0.0
upper = 157.31.255.255
seancfoley commented 10 months ago

to drop the prefix length from the resulting lower and upper, you can also do:

String ip = "157.0.50.55/11";
IPAddress ipAddress = new IPAddressString(ip).getAddress().toPrefixBlock().withoutPrefixLength();
System.out.println("lower = " + ipAddress.getLower());
System.out.println("upper = " + ipAddress.getUpper());
YooLin commented 9 months ago

157.0.50.55/11

thanks a lot! that was very helpful for me.