Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: convert int to InetAddress

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?

like image 451
kdt Avatar asked Dec 24 '09 09:12

kdt


1 Answers

Tested and working:

int ip  = ... ; String ipStr =    String.format("%d.%d.%d.%d",          (ip & 0xff),             (ip >> 8 & 0xff),                       (ip >> 16 & 0xff),              (ip >> 24 & 0xff)); 
like image 145
hbr Avatar answered Sep 21 '22 18:09

hbr