Home > other >  Convert Integer to IP address
Convert Integer to IP address

Time:02-06

I've got two ip addresses representing by String.

String first = "192.168.0.1";
String second = "192.168.0.5";

I need to sort it out like:

192.168.0.5
192.168.0.4
192.168.0.3
192.168.0.2

Here is my code so far:

 private static void method(String first, String second){
        String tempFirst = first.replace(".", "");
        String tempSecond = second.replace(".", "");
        int ipFirst = Integer.valueOf(tempFirst);
        int ipSecond = Integer.valueOf(tempSecond);

        while (ipSecond > ipFirst){
            System.out.println(ipSecond);
            ipSecond--;
        }
    }

I need to convert output from these method:

19216805
19216804
19216803
19216802

to:

192.168.0.5
192.168.0.4
192.168.0.3
192.168.0.2

How to achieve it?

CodePudding user response:

You can't. Does 19216802 mean 192.168.0.2 or 192.16.80.2?

An IP address can just be thought of as an unsigned 32bit number, so your 192.168.0.2 can be seen as 192 * 2 ^ 24 168 * 2 ^ 16 0 ^ 8 2 = 3232235522

What you really want to do is convert it to a 32bit number, sort, then convert back.

CodePudding user response:

My solution:

private static void ipAddress(String first, String second) throws UnknownHostException {
    byte[] bytesFirst = InetAddress.getByName(first).getAddress();
    byte[] bytesSecond = InetAddress.getByName(second).getAddress();
    int fIP = new BigInteger(1, bytesFirst).intValue();
    int sIP = new BigInteger(1, bytesSecond).intValue();

    while (sIP > fIP){
        int ip  = sIP ;
        String ipStr =
                String.format("%d.%d.%d.%d",
                        (ip >> 24 & 0xff),
                        (ip >> 16 & 0xff),
                        (ip >> 8 & 0xff),
                        (ip & 0xff));
        System.out.println(ipStr);
        sIP--;
    }
}

CodePudding user response:

I think the way you wish to achieve this is a bad way of doing it. My personal solution would be implement a class for Ip Addresses:

public class IpAddress implements Comparable<IpAddress> {
        public int A, B, C, D; // A.B.C.D (A,B,C,D are all 8 bit numbers)

        public IpAddress(String Ip) {
            this.A = Integer.valueOf(Ip.split("\\.")[0]);
            this.B = Integer.valueOf(Ip.split("\\.")[1]);
            this.C = Integer.valueOf(Ip.split("\\.")[2]);
            this.D = Integer.valueOf(Ip.split("\\.")[3]);
        }

        public int compareTo(IpAddress other) {
            return (this.A - other.A) * 16581375   (this.B - other.B) * 65025   (this.C - other.C) * 255   (this.D - other.D);
        }

        public String toString() {
            return this.A   "."   this.B   "."   this.C   "."   this.D;
        }

        public void decrement() {
            this.D--;
            if (this.D < 0) {
                this.D = 255;
                this.C--;
                if (this.C < 0) {
                this.C = 255;
                    this.B--;
                    if (this.B < 0) {
                        this.B = 255;
                        this.A--;
                        if (this.A < 0)
                            this.A = 255;
                    }
                }
            }
        }

        public boolean isGreaterThan(IpAddress other) {
            return (this.compareTo(other) > 0);
        }
  }

And for your method do:

private static void method(String first, String second){
    IpAddress ipFirst = new IpAddress(first);
    IpAddress ipSecond = new IpAddress(second);

    while (ipSecond.isGreaterThan(ipFirst)){
        System.out.println(ipSecond.toString());
        ipSecond.decrement();
    }
}
  •  Tags:  
  • Related