Java: convert int to InetAddress

懵懂的女人 提交于 2019-12-17 22:57:22

问题


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?


回答1:


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.




回答2:


Tested and working:

int ip  = ... ;
String ipStr = 
  String.format("%d.%d.%d.%d",
         (ip & 0xff),   
         (ip >> 8 & 0xff),             
         (ip >> 16 & 0xff),    
         (ip >> 24 & 0xff));



回答3:


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;
    }
}



回答4:


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;




回答5:


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




回答6:


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.




回答7:


Using Google Guava:

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

InetAddress address = InetAddress.getByAddress(bytes);




回答8:


This may work try


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




回答9:


  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);
  }


来源:https://stackoverflow.com/questions/1957637/java-convert-int-to-inetaddress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!