可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use Java (with Spring framework) and want to convert between numeric representations of IPv4 addresses (e.g. 2130706433) and their textual counterparts (e.g. 127.0.0.1
). Often, methods for doing this are supplied in programming languages (they're usually called INET_NTOA
and INET_ATON
respectively) but I can't find it in Java.
Anybody knows what they're called or how to implement them?
回答1:
Look at InetAddress in the javadocs. These functions are not directly supported by the Standard API but you can extract both representations using this class. A small example:
InetAddress address = InetAddress.getLocalHost(); byte[] byteAddress = address.getAddress(); System.out.println(Arrays.toString(byteAddress)); System.out.println(address.getHostAddress());
(Keep in mind that bytes are signed.)
If you have long-s than yo can use ByteBuffer, for fast and comfortable coversion. Methods: putLong() then array().
回答2:
java.net.InetAddress.getByAddress(byte[])
Not exactly same as INET_NTOA, but very similar to.
Example with long argument:
String ntoa(long raw) { byte[] b = new byte[] {(byte)(raw >> 24), (byte)(raw >> 16), (byte)(raw >> 8), (byte)raw}; try { return InetAddress.getByAddress(b).getHostAddress(); } catch (UnknownHostException e) { //No way here return null; } }
回答3:
I guess the InetAddress will do what you want
回答4:
Here's what I wrote myself to get a numeric representation of a textual IPv4 address:
public static Long ipAsNumeric(String ipAsString) { String[] segments = ipAsString.split("\\."); return (long) (Long.parseLong(segments[0]) * 16777216L + Long.parseLong(segments[1]) * 65536L + Long.parseLong(segments[2]) * 256L + Long.parseLong(segments[3])); }
Of course, this assumes the IPv4 address is given on a valid format.
回答5:
Guava's InetAddresses will do the trick.
Look at Ido's comment in Going from 127.0.0.1 to 2130706433, and back again
回答6:
Using the IPAddress Java library it is simple, one line of code for each direction works for both IPv4 and IPv6. In fact, you can write code that works for both IPv4 and IPv6 as in the first example below. Disclaimer: I am the project manager of that library.
IP-version agnostic using byte[] and/or BigInteger:
IPAddress loopback = new IPAddressString("::1").getAddress(); System.out.println(loopback.getValue()); IPAddress backAgain = new IPAddressGenerator().from(loopback.getBytes()); System.out.println(backAgain);
Use ints for IPv4:
IPv4Address loopbackv4 = new IPAddressString("127.0.0.1").getAddress().toIPv4(); System.out.println(loopbackv4.intValue()); IPv4Address backAgainv4 = new IPv4Address(loopbackv4.intValue()); System.out.println(backAgainv4);
Use BigInteger for IPv6:
IPv6Address loopbackv6 = new IPAddressString("::1").getAddress().toIPv6(); System.out.println(loopbackv6.getValue()); IPv6Address backAgainv6 = new IPv6Address(loopbackv6.getValue()); System.out.println(backAgainv6);
Output:
1 0:0:0:0:0:0:0:1 2130706433 127.0.0.1 1 0:0:0:0:0:0:0:1