2015年8月6日 星期四

[java] IP Address to byte array

public byte[] asBytes(String addr) {
// Convert the TCP/IP address string to an integer value
int ipInt = parseNumericAddress(addr);
if (ipInt == 0)
return null;
// Convert to bytes
byte[] ipByts = new byte[4];
ipByts[3] = (byte) (ipInt & 0xFF);
ipByts[2] = (byte) ((ipInt >> 8) & 0xFF);
ipByts[1] = (byte) ((ipInt >> 16) & 0xFF);
ipByts[0] = (byte) ((ipInt >> 24) & 0xFF);
// Return the TCP/IP bytes
return ipByts;
}

沒有留言:

張貼留言