[Java]IP文字列を整数に変換するコード

Javaコード:
public int iptoIntFunc(String ip){
if(null == ip || “".equals(ip.trim()) || !checkIP(ip)) return 0;
String[] ipStrs = ip.split(“\\.");
int result = 0;
for(int i = 0;i < 4;i ++){ Integer ipSubInteger = Integer.parseInt(ipStrs[i]); if(ipSubInteger > 255) {
result = 0;
break;
}
result += (ipSubInteger << (24 – i * 8));
}
return result;
}

/*正規表現でipチェック*/
private boolean checkIP(String ip){
return ip.matches(“\\d{1,3}(\\.\\d{1,3}){3}");
}

Java

Posted by arkgame