Java: convert int to InetAddress

后端 未结 10 1232
既然无缘
既然无缘 2020-12-14 06:34

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 <

10条回答
  •  感动是毒
    2020-12-14 07:24

    Tested and working:

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

提交回复
热议问题