有谁知道如何在Java中将IP地址的十进制表示法转换为二进制形式?请让我知道......
答案 0 :(得分:7)
写为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。
答案 1 :(得分:3)
您可以使用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 :(得分:1)
收集你的建议和其他一些来源,我发现有用的是将InetAdress转换为bit数组,以及BitSet,它可以帮助计算和二进制表示中的(),或(),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;
}
}
答案 3 :(得分:0)
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