将整数转换为 IP 地址

Convert Integer to IP address

我有两个 ip 地址代表 String

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

我需要这样整理:

192.168.0.5
192.168.0.4
192.168.0.3
192.168.0.2

到目前为止,这是我的代码:

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

我需要转换这些方法的输出:

19216805
19216804
19216803
19216802

至:

192.168.0.5
192.168.0.4
192.168.0.3
192.168.0.2

如何实现?

你不能。 19216802 是指 192.168.0.2 还是 192.16.80.2

一个IP地址可以被认为是一个无符号的32位数字,所以你的192.168.0.2可以被看作192 * 2 ^ 24 + 168 * 2 ^ 16 + 0 ^ 8 + 2 = 3232235522

您真正想做的是将其转换为 32 位数字,排序,然后再转换回来。

我认为您希望实现此目的的方式很糟糕。 我个人的解决方案是为 IP 地址实施 class:

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

对于您的方法,请执行以下操作:

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

我的解决方案:

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