java IP地址互转byte[4]数组
举例IP地址为:192.168.1.24
转为byte[4] : [-64,-88,1,24]
public static byte[] ipToByteArray(String ip) {
if (StringUtils.isBlank(ip)) {
return null;
}
String[] split = ip.split("\.");
byte[] bs = new byte[4];
for (int i=0; i < split.length; i++) {
bs[i] = (byte)Integer.parseInt(split[i]);
}
return bs;
}
public static String byteArrayToIp(byte[] bs) {
StringBuilder sb = new StringBuilder();
for (int i=0 ; i < bs.length; i++) {
if(i != bs.length - 1){
sb.append(bs[i] & 0xff).append(".");
}else{
sb.append(bs[i] & 0xff);
}
}
return sb.toString();
}


![java IP地址互转byte[4]数组 java IP地址互转byte[4]数组](http://www.mshxw.com/aiimages/31/704724.png)
