7

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?

Gruber
  • 4,478
  • 6
  • 47
  • 74

7 Answers7

4

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
Sean F
  • 4,344
  • 16
  • 30
4

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().

zeller
  • 4,904
  • 2
  • 22
  • 40
  • Thanks, seems like a fruitful way to explore. Seems like there are no methods which do what I want right away. – Gruber Feb 03 '12 at 13:53
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.

Gruber
  • 4,478
  • 6
  • 47
  • 74
3

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;
    }
}
lxbndr
  • 2,159
  • 1
  • 15
  • 17
  • Thanks. Would be nice to have a function that takes a `long` as argument. – Gruber Feb 03 '12 at 13:50
  • You can use [ByteBuffer](http://docs.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html). Methods: putLong() then array() – zeller Feb 03 '12 at 13:58
2

I guess the InetAddress will do what you want

Kris
  • 5,714
  • 2
  • 27
  • 47
0

Guava's InetAddresses will do the trick.

Look at Ido's comment in Going from 127.0.0.1 to 2130706433, and back again

Community
  • 1
  • 1
Agustí Sánchez
  • 10,455
  • 2
  • 34
  • 25
0

Just try with the following code.

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpConvertUtil {
    public static long inetAton(String ipAddress) throws UnknownHostException {
        
        InetAddress address = InetAddress.getByName(ipAddress);
        byte[] bytes = address.getAddress();
        long result = 0;
        for (byte b : bytes) {
            result = result << 8 | (b & 0xFF);
        }
        return result;
        
    }
    
    public static String inetNtoa(long address) {
        return (address >> 24 & 0xFF) + "." +
                (address >> 16 & 0xFF) + "." +
                (address >> 8 & 0xFF) + "." +
                (address & 0xFF);
    }
}
Jimmy Jiang
  • 1
  • 1
  • 7