INET_NTOA and INET_ATON in Java?

I am using Java (with Spring framework) and want to convert between numeric representations of IPv4 addresses (e.g. 2130706433) and their text counterparts (e.g. 127.0.0.1 ). Often methods for this are provided in programming languages ​​(they are usually called INET_NTOA and INET_ATON respectively), but I can not find them in Java.

Does anyone know what they call or how to implement them?

+6
source share
6 answers

Check out InetAddress at javadocs. These functions are not directly supported by the standard API, but you can extract both views 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, then yo can use ByteBuffer for quick and convenient coversion. Methods: putLong (), then array ().

+4
source

java.net.InetAddress.getByAddress (byte [])

Not the same as INET_NTOA, but very similar to.

An example with a 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
source

I think InetAddress will do what you want.

+2
source

Here is what I wrote to myself to get a numerical representation of a text 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 that the IPv4 address is in a valid format.

+2
source

Guava InetAddresses will do the trick.

See Ido's comment on Migrating from 127.0.0.1 to 2130706433 and vice versa

0
source

Using the IPAddress Java library, it's 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 this 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); 

Conclusion:

  1 0:0:0:0:0:0:0:1 2130706433 127.0.0.1 1 0:0:0:0:0:0:0:1 
0
source

Source: https://habr.com/ru/post/907570/


All Articles