Language agnostic 如何将IP地址的十进制表示形式转换为二进制?

Language agnostic 如何将IP地址的十进制表示形式转换为二进制?,language-agnostic,ip-address,Language Agnostic,Ip Address,有人知道如何在Java中将IP地址的十进制表示法转换成二进制形式吗?请让我知道…写为a.b.c.d的IP地址可以转换为32位整数值 使用运算符作为 (a << 24) | (b << 16) | (c << 8) | d (a您可以使用该类。您应该查看的两个方法是getByName和getAddress。下面是一个简单的代码示例 import java.net.InetAddress; import java.net.UnknownHostException

有人知道如何在Java中将IP地址的十进制表示法转换成二进制形式吗?请让我知道…

写为
a.b.c.d
的IP地址可以转换为32位整数值
使用运算符作为

(a << 24) | (b << 16) | (c << 8) | d
(a您可以使用该类。您应该查看的两个方法是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();

通过收集您的建议和其他一些来源,我发现将inetAddress转换为位数组和位集非常有用,这有助于从二进制表示中计算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;
}
公共类IpConverter{
公共静态void main(字符串[]args){
字符串source=“192.168.1.1”;
INETAddressIP=null;
试一试{
ip=InetAddress.getByName(源);
}捕获(未知后异常e){
e、 printStackTrace();
返回;
}
System.out.println(“来源:+ip”);
//到位序列------------
字节[]二进制ip=ip.getAddress();
比特集[]比特集=新比特集[binaryIP.length];
int k=0;
系统输出打印(“到二进制:”;
for(字节b:binaryIP){
比特集[k]=byteToBitSet(b);
系统输出打印(toString(位集[k])+”;
k++;
}
System.out.println();
//返回InetAddress---------
字节[]BinaryP2=新字节[4];
k=0;
用于(位集b:位集){
binaryIP2[k]=比特集字节(b);
k++;
}
InetAddress ip2=null;
试一试{
ip2=InetAddress.getByAddress(二进制p2);
}捕获(未知后异常e){
e、 printStackTrace();
返回;
}
System.out.println(“翻转回:”+ip2);
}
公共静态位集byteToBitSet(字节b){
位集位=新位集(8);
对于(int i=0;i<8;i++){
bits.set(i,((b&)(1可以为您执行此操作。它可以解析各种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

0和255不是有效的IP地址组件。@Vladimir,不总是,但您会调用
192.168.0.1
有效还是无效?这就是我给出验证IP地址引用的原因。我理解您的意思是
a.b.c.0
a.b.c.255
不是完全有效的地址。如果您使用192.168.0.0/22,192.168.0.255不是有效地址吗?