Java:将 int 转换为 InetAddress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:47:14  来源:igfitidea点击:

Java: convert int to InetAddress

javainetaddress

提问by kdt

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

我有一个int包含网络字节顺序的 IP 地址,我想将其转换为InetAddress对象。我看到有一个InetAddress构造函数需要 a byte[],是否有必要先将the 转换int为 a byte[],还是有其他方法?

采纳答案by skaffman

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.

您可能需要交换字节数组的顺序,我不知道数组是否会以正确的顺序生成。

回答by valli

This may work try

这可能会奏效


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

回答by Dennis Laumen

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

没有足够的声誉来评论 skaffman 的答案,因此我将其添加为单独的答案。

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

skaffman 提出的解决方案是正确的,只有一个例外。BigInteger.toByteArray() 返回一个可能有前导符号位的字节数组。

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.

PS 上面的代码使用来自 Apache Commons Lang 的 ArrayUtils。

回答by aalmeida

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

回答by hbr

Tested and working:

测试和工作:

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

回答by SHUVA JYOTI KAR

Using Google Guava:

使用谷歌番石榴:

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

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

InetAddress address = InetAddress.getByAddress(bytes);

InetAddress 地址 = InetAddress.getByAddress(bytes);

回答by user2660727

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

回答by aij

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

如果您使用的是 Google 的 Guava 库,InetAddresses.fromInteger那么完全可以满足您的需求。Api 文档在这里

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

如果您更愿意编写自己的转换函数,则可以执行类似于@aalmeida 建议的操作,但一定要按正确的顺序放置字节(最重要的字节在前)。

回答by bowman han

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;

您可以使用此函数将 int 转换为字节;