Java: convert int to InetAddress
Asked Answered
U

10

31

I have an int which contains an IP address in network byte order, which I would like to convert to an InetAddress object. I see that there is an InetAddress constructor that takes a byte[], is it necessary to convert the int to a byte[] first, or is there another way?

Unmitigated answered 24/12, 2009 at 9:59 Comment(3)
Can you post an example how this int look like and how its string representation should look like? I can't imagine how to put 255255255255 in an int, it would overflow.Unmuzzle
@BalusC: A IPv4 address is just a 32 bit number, it's just that it's usually represented as 4 8-bit values. The information fits just fine in 32 bits, though.Glassful
Remember that, if you want to ever support IPv6, you can't use single int to handle IP addresses.Castora
G
17

This should work:

int ipAddress = ....
byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
InetAddress address = InetAddress.getByAddress(bytes);

You might have to swap the order of the byte array, I can't figure out if the array will be generated in the correct order.

Glassful answered 24/12, 2009 at 10:6 Comment(4)
That does indeed still require swapping the order of the byte array. However, it turns out my input was actually in host order after all! Thanks.Unmitigated
will not work for addresses in the range 0.0.0.0 - 0.127.255.255 and 255.128.0.0 - 255.255.255.255: bytes will have less than 4 elementsPharaoh
@NikolayKuznetsov Because those ranges would result in a byte array of size 3 or less, whereas InetAddress requires the array to be of size 4 or 16.Sloane
Dangerous. Does not work, for instance for 0.0.0.0. Not reliable!Samsun
U
30

Tested and working:

int ip  = ... ;
String ipStr = 
  String.format("%d.%d.%d.%d",
         (ip & 0xff),   
         (ip >> 8 & 0xff),             
         (ip >> 16 & 0xff),    
         (ip >> 24 & 0xff));
Unbiased answered 20/5, 2013 at 4:5 Comment(1)
This converts the bytes in the wrong order. It would work for little-endian ints, but the question asked about "network byte order" which is big-endian as are ints in Java. The correct order is: String.format("%d.%d.%d.%d", (ip >> 24 & 0xff), (ip >> 16 & 0xff), (ip >> 8 & 0xff), (ip & 0xff));Drake
G
17

This should work:

int ipAddress = ....
byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
InetAddress address = InetAddress.getByAddress(bytes);

You might have to swap the order of the byte array, I can't figure out if the array will be generated in the correct order.

Glassful answered 24/12, 2009 at 10:6 Comment(4)
That does indeed still require swapping the order of the byte array. However, it turns out my input was actually in host order after all! Thanks.Unmitigated
will not work for addresses in the range 0.0.0.0 - 0.127.255.255 and 255.128.0.0 - 255.255.255.255: bytes will have less than 4 elementsPharaoh
@NikolayKuznetsov Because those ranges would result in a byte array of size 3 or less, whereas InetAddress requires the array to be of size 4 or 16.Sloane
Dangerous. Does not work, for instance for 0.0.0.0. Not reliable!Samsun
A
4

I think that this code is simpler:

static public byte[] toIPByteArray(int addr){
        return new byte[]{(byte)addr,(byte)(addr>>>8),(byte)(addr>>>16),(byte)(addr>>>24)};
    }

static public InetAddress toInetAddress(int addr){
    try {
        return InetAddress.getByAddress(toIPByteArray(addr));
    } catch (UnknownHostException e) {
        //should never happen
        return null;
    }
}
Alphonsa answered 19/1, 2013 at 19:50 Comment(1)
This is almost right, but you got the order of the bytes backwards: docs.oracle.com/javase/7/docs/api/java/net/…Sloane
S
4

If you're using Google's Guava libraries, InetAddresses.fromInteger does exactly what you want. Api docs are here

If you'd rather write your own conversion function, you can do something like what @aalmeida suggests, except be sure to put the bytes in the right order (most significant byte first).

Sloane answered 3/4, 2014 at 20:55 Comment(2)
the link in your answer is brokenBigeye
@Bigeye Updated.Sloane
N
2
public static byte[] int32toBytes(int hex) {
    byte[] b = new byte[4];
    b[0] = (byte) ((hex & 0xFF000000) >> 24);
    b[1] = (byte) ((hex & 0x00FF0000) >> 16);
    b[2] = (byte) ((hex & 0x0000FF00) >> 8);
    b[3] = (byte) (hex & 0x000000FF);
    return b;

}

you can use this function to turn int to bytes;

Nazi answered 27/8, 2014 at 6:32 Comment(0)
E
1

Not enough reputation to comment on skaffman's answer so I'll add this as a separate answer.

The solution skaffman proposes is correct with one exception. BigInteger.toByteArray() returns a byte array which could have a leading sign bit.

byte[] bytes = bigInteger.toByteArray();

byte[] inetAddressBytes;

// Should be 4 (IPv4) or 16 (IPv6) bytes long
if (bytes.length == 5 || bytes.length == 17) {
    // Remove byte with most significant bit.
    inetAddressBytes = ArrayUtils.remove(bytes, 0);
} else {
    inetAddressBytes = bytes;
}

InetAddress address = InetAddress.getByAddress(inetAddressBytes);

PS above code uses ArrayUtils from Apache Commons Lang.

Export answered 21/1, 2010 at 15:34 Comment(3)
I don't think an additional leading sign bit will be a problem, but missing bytes if the address is in the range 0.0.0.0 - 0.127.255.255 and 255.128.0.0 - 255.255.255.255Pharaoh
Sorry, I'm not an expert in this field. Could you elaborate on your comment? I'm afraid I'm missing the point (and could potentially have a bug in my code ;) ).Export
@DennisLaumen You do. Try for example, 0, 42, or -42. You should get 0.0.0.0, 0.0.0.42, and 255.255.255.214. Instead you'll get an exception. See also en.wikipedia.org/wiki/Twos_complementSloane
H
1

Using Google Guava:

byte[] bytes =Ints.toByteArray(ipAddress);

InetAddress address = InetAddress.getByAddress(bytes);

Hebephrenia answered 15/1, 2014 at 11:30 Comment(0)
M
0

Since comments cannot be formatted, let me post the code derived from the comment by @Mr.KevinThomas:

if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
    ipAddress = Integer.reverseBytes(ipAddress);
}
sReturn = String.format(Locale.US, "%d.%d.%d.%d", (ipAddress >> 24 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 8 & 0xff), (ipAddress & 0xff));

It has been tested on Android.

Mathamathe answered 4/9, 2020 at 13:13 Comment(0)
S
-1

This may work try


public static String intToIp(int i) {
        return ((i >> 24 ) & 0xFF) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >>  8 ) & 0xFF) + "." +
               ( i        & 0xFF);
    }

Seaden answered 24/12, 2009 at 10:10 Comment(1)
He was asking for int-to-bytearray, not int-to-string. Also your words "this may work try" makes me think that you just googled blind and copypasted random function? Why?Unmuzzle
Y
-1
  public InetAddress intToInetAddress(Integer value) throws UnknownHostException
  {
    ByteBuffer buffer = ByteBuffer.allocate(32);
    buffer.putInt(value);
    buffer.position(0);
    byte[] bytes = new byte[4];
    buffer.get(bytes);
    return InetAddress.getByAddress(bytes);
  }
Yuriyuria answered 27/2, 2014 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.