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