【问题标题】:Convert Integer to IP address将整数转换为 IP 地址
【发布时间】:2022-02-04 21:09:45
【问题描述】:

我有两个由String 表示的IP 地址。

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

如何实现?

【问题讨论】:

  • 192.168.10.1 > 192.168.9.1
  • 您的邮件可能是XY Problem,您在其中询问如何解决特定代码问题,而最佳解决方案是使用完全不同的方法。我自己,我会创建自己的 public class IpAddress implements Comparable<IpAddress> 类,它包含 4 个整数,并以适当的方式实现 compareTo。
  • 只需将其转换为 32 位数字,排序,然后再转换回来。
  • 我会在“.”上拆分字符串,而不是替换。将每个元素转换为整数。然后将第一个元素乘以 10 亿,第二个元素乘以一百万,第三个元素乘以 1000,然后将 4 相加。然后可以安全地对它们进行排序。
  • 我的错,我误读了这个问题,不过我会留下previous dupe 作为链接。 排序可能是用词不当。您可能的意思是:打印两个给定的所有 IP 地址,或者其他什么..

标签: java string integer


【解决方案1】:

你不能。 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 位数字,排序,然后再转换回来。

【讨论】:

  • 我认为他们首先使用“。”
  • 意思是 192.168.0.2
  • @Maxqueue 他要求将19216802 转换为192.168.0.2,你不能这样做。
  • @Bib 他/她以 String first = "192.168.0.1" 开头;字符串秒 = "192.168.0.5";
  • @Maxqueue 请阅读他的问题,他要求将 19216802 转换为 192.168.0.2。他看错了。
【解决方案2】:

我的解决方案:

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

【讨论】:

  • 欢迎来到 StackOverflow!简要说明为什么这项工作对其他人有用。
【解决方案3】:

我认为您希望实现这一目标的方式是一种糟糕的方式。 我个人的解决方案是为 IP 地址实现一个类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2014-03-03
    • 2011-02-25
    • 2012-10-26
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多