java Convert a string IPv4 IP address to the equivalent long numeric value.

java Convert a string IPv4 IP address to the equivalent long numeric value.

To convert a string IPv4 IP address to the equivalent long numeric value in Java, you can use the InetAddress.getByName method to convert the string to an InetAddress object, and then use the getAddress method to get the raw bytes of the IP address. You can then use the ByteBuffer.wrap method to wrap the bytes in a ByteBuffer, and then use the getLong method to convert the bytes to a long numeric value.

Here is an example of how you can convert a string IPv4 IP address to the equivalent long numeric value in Java:

String ip = "192.168.0.1";
InetAddress inet = InetAddress.getByName(ip);
byte[] bytes = inet.getAddress();
long value = ByteBuffer.wrap(bytes).getLong();
‮w:ecruoS‬ww.lautturi.com

In this example, we have a string called ip that contains an IPv4 IP address. We use the InetAddress.getByName method to convert the string to an InetAddress object, and then use the getAddress method to get the raw bytes of the IP address. We use the ByteBuffer.wrap method to wrap the bytes in a ByteBuffer, and then use the getLong method to convert the bytes to a long numeric value.

Created Time:2017-11-03 00:14:42  Author:lautturi