Java 从 127.0.0.1 到 2130706433,然后再返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2241229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Going from 127.0.0.1 to 2130706433, and back again
提问by knorv
Using the standard Java libraries, what is the quickest way to get from the dotted string representation of an IPV4-address ("127.0.0.1"
) to the equivalent integer representation (2130706433
).
使用标准 Java 库,从 IPV4 地址的虚线字符串表示 ( "127.0.0.1"
) 到等效整数表示 ( 2130706433
)的最快方法是什么?
And correspondingly, what is the quickest way to invert said operation - going from the integer 2130706433
to the string representation"127.0.0.1"
?
相应地,反转所述操作的最快方法是什么 - 从整数2130706433
到字符串表示"127.0.0.1"
?
采纳答案by Geoff Reedy
String to int:
字符串转int:
int pack(byte[] bytes) {
int val = 0;
for (int i = 0; i < bytes.length; i++) {
val <<= 8;
val |= bytes[i] & 0xff;
}
return val;
}
pack(InetAddress.getByName(dottedString).getAddress());
Int to string:
整数到字符串:
byte[] unpack(int bytes) {
return new byte[] {
(byte)((bytes >>> 24) & 0xff),
(byte)((bytes >>> 16) & 0xff),
(byte)((bytes >>> 8) & 0xff),
(byte)((bytes ) & 0xff)
};
}
InetAddress.getByAddress(unpack(packedBytes)).getHostAddress()
回答by Brian Agnew
I've not tried it wrt. performance, but the simplest way is probably to use the NIO ByteBuffer.
我没试过。性能,但最简单的方法可能是使用 NIO ByteBuffer。
e.g.
例如
byteBuffer.put(integer).array();
would return you a byte array representing the integer. You mayneed to modify the byte order.
将返回一个表示整数的字节数组。您可能需要修改字节顺序。
回答by William Brendel
I've modified my original answer. In Sun's implementation of InetAddress
, the hashCode
method produces the integer representation of the IPv4 address, but as the commenters correctly pointed out, this is not guaranteed by the JavaDoc. Therefore, I decided to use the ByteBuffer
class to calculate the value of the IPv4 address instead.
我已经修改了我的原始答案。在 Sun 的 实现中InetAddress
,该hashCode
方法生成 IPv4 地址的整数表示,但正如评论者正确指出的那样,JavaDoc 不保证这一点。因此,我决定使用ByteBuffer
该类来计算 IPv4 地址的值。
import java.net.InetAddress;
import java.nio.ByteBuffer;
// ...
try {
// Convert from integer to an IPv4 address
InetAddress foo = InetAddress.getByName("2130706433");
String address = foo.getHostAddress();
System.out.println(address);
// Convert from an IPv4 address to an integer
InetAddress bar = InetAddress.getByName("127.0.0.1");
int value = ByteBuffer.wrap(bar.getAddress()).getInt();
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
The output will be:
输出将是:
127.0.0.1
2130706433
回答by Abei Villafane
In case you need to learn the long hand math, you can use Substr to rip out the octets. Mutliply the first octet signifying the Class by (256*256*256) or (2^24) second multiplied by (256*256) (2^16) third multiplied by (256) (2^8) fourth multiplied by 1 or (2^0)
如果您需要学习长手数学,您可以使用 Substr 来删除八位字节。第一个八位字节乘以 (256*256*256) 或 (2^24) 第二乘以 (256*256) (2^16) 第三乘以 (256) (2^8) 第四乘以 1 或(2^0)
127 * (2^24) + 0 *(2^16) + 0 * (2^8) + 1 * (2^0) 2130706432 + 0 + 0 + 1 = 2130706433
127 * (2^24) + 0 *(2^16) + 0 * (2^8) + 1 * (2^0) 2130706432 + 0 + 0 + 1 = 2130706433
回答by Ido
You can also use the Google Guava InetAddressClass
您还可以使用Google Guava InetAddress类
String ip = "192.168.0.1";
InetAddress addr = InetAddresses.forString(ip);
// Convert to int
int address = InetAddresses.coerceToInteger(addr);
// Back to str
String addressStr = InetAddresses.fromInteger(address));
回答by Sathish Kumar S
Another way:
其它的办法:
public static long ipToLong(String ipAddress) {
String[] ipAddressInArray = ipAddress.split("\.");
long result = 0;
for (int i = 0; i < ipAddressInArray.length; i++) {
int power = 3 - i;
int ip = Integer.parseInt(ipAddressInArray[i]);
result += ip * Math.pow(256, power);
}
return result;
}
回答by Sean F
Using the IPAddress Java libraryit is simple, one line of code for each direction. Disclaimer: I am the project manager of that library.
使用IPAddress Java 库很简单,每个方向一行代码。免责声明:我是那个图书馆的项目经理。
IPv4Address loopback = new IPAddressString("127.0.0.1").getAddress().toIPv4();
System.out.println(loopback.intValue());
IPv4Address backAgain = new IPv4Address(loopback.intValue());
System.out.println(backAgain);
Output:
输出:
2130706433
127.0.0.1