【问题标题】:How can I convert the decimal representation of an IP address into binary?如何将 IP 地址的十进制表示形式转换为二进制?
【发布时间】:2009-07-18 02:57:03
【问题描述】:

有谁知道如何在 Java 中将 IP 地址的十进制表示法转换为二进制形式?请告诉我...

【问题讨论】:

    标签: language-agnostic ip-address


    【解决方案1】:

    您可以使用java.net.InetAddress 类。您应该查看的两个方法是 getByName 和 getAddress。这是一个简单的代码示例

    import java.net.InetAddress;
    import java.net.UnknownHostException;
    /* ... */
    String ip = "192.168.1.1";
    InetAddress address = null;
    try {
      address = InetAddress.getByName(ip);
    } catch (UnknownHostException e) {
      //Your String wasn't a valid IP Address or host name
    }
    byte [] binaryIP = address.getAddress();
    

    【讨论】:

      【解决方案2】:

      写成a.b.c.d的IP地址可以转换为32位整数值
      使用shift and bit-wise inclusive OR 运算符作为,

      (a << 24) | (b << 16) | (c << 8) | d
      

      为了安全起见,每个a,b,c,d 都有有效范围0-255 - 您可以在转换中检查这一点。
      您可以进一步validate the IP address using this regex example

      【讨论】:

      • 0 和 255 不是有效的 IP 地址组件。
      • @Vladimir,不总是,但你会打电话给192.168.0.1 有效还是无效?这就是为什么我给出了验证 IP 地址参考。我了解您的意思是 a.b.c.0a.b.c.255 不是完全有效的地址。
      • 如果您使用 192.168.0.0/22,192.168.0.255 不是有效地址吗?
      【解决方案3】:

      收集您的建议和其他一些来源后,我发现将 In​​etAdress 转换为位数组以及 BitSet 很有用,它可以帮助您从二进制表示中计算 and()、or()、xor() .

      以下示例显示了如何将 ip 转换为二进制和二进制到 ip。

      享受吧!

      public class IpConverter {
      public static void main(String[] args) {
          String source = "192.168.1.1";
          InetAddress ip = null;
          try {
              ip = InetAddress.getByName(source);
          } catch (UnknownHostException e) {
              e.printStackTrace();
              return;
          }
          System.out.println( "source : " + ip);
      
          // To bit sequence ------------
          byte[] binaryIP = ip.getAddress();
          BitSet[] bitsets = new BitSet[binaryIP.length];
          int k = 0;
      
          System.out.print("to binary: ");
          for (byte b : binaryIP) {
              bitsets[k] = byteToBitSet(b);
              System.out.print( toString( bitsets[k] ) + ".");
              k++;
          }
          System.out.println();
      
          // Back to InetAdress ---------
          byte[] binaryIP2 = new byte[4];
          k = 0;
          for (BitSet b : bitsets) {
              binaryIP2[k] = bitSetToByte(b);
              k++;
          }
      
          InetAddress ip2 = null;
          try {
              ip2 = InetAddress.getByAddress(binaryIP2);
          } catch (UnknownHostException e) {
              e.printStackTrace();
              return;
          }
      
          System.out.println( "flipped back to : " + ip2);
      }
      
      public static BitSet byteToBitSet(byte b) {
          BitSet bits = new BitSet(8);
          for (int i = 0; i < 8; i++) {
              bits.set(i, ((b & (1 << i)) != 0) );
          }
          return bits;
      }
      
      public static byte bitSetToByte(BitSet bits) {
          int value = 0;
          for (int i = 0; i < 8; i++) {
              if (bits.get(i) == true) {
                  value = value | (1 << i);
              }
          }
          return (byte) value;
      }
      
      public static byte bitsToByte(boolean[] bits) {
          int value = 0;
          for (int i = 0; i < 8; i++) {
              if (bits[i] == true) {
                  value = value | (1 << i);
              }
          }
          return (byte) value;
      }
      
      public static boolean[] byteToBits(byte b) {
          boolean[] bits = new boolean[8];
          for (int i = 0; i < bits.length; i++) {
              bits[i] = ((b & (1 << i)) != 0);
          }
          return bits;
      }
      
      public static String toString(BitSet bits){
          String out = "";
          for (int i = 0; i < 8; i++) {
              out += bits.get(i)?"1":"0";         
          }
          return out;
      }
      

      }

      【讨论】:

        【解决方案4】:

        open-source IPAddress Java library 可以为您做到这一点。它可以解析各种 IP 地址格式,包括 IPv4 或 IPv6,并具有生成各种字符串格式的方法,包括一种二进制格式。免责声明:我是 IPAddress 库的项目经理。

        这段代码可以做到:

        static void convert(String str) {
            IPAddressString string = new IPAddressString(str);
            IPAddress addr = string.getAddress();
            System.out.println(addr + " in binary is " + addr.toBinaryString());
        }
        

        例子:

        convert("1.2.3.4");
        convert("a:b:c:d:e:f:a:b");
        

        输出是:

        1.2.3.4 in binary is 00000001000000100000001100000100
        a:b:c:d:e:f:a:b in binary is 00000000000010100000000000001011000000000000110000000000000011010000000000001110000000000000111100000000000010100000000000001011
        

        【讨论】:

          猜你喜欢
          • 2013-11-13
          • 1970-01-01
          • 2019-05-07
          • 1970-01-01
          • 1970-01-01
          • 2013-11-16
          • 2018-02-03
          • 2017-04-23
          • 1970-01-01
          相关资源
          最近更新 更多