/**
* 将字符串的ip地址转换为long型.
*
* @param ip ip地址
* @return long 型ip地址
*/
public static final Long ip2Long(final String ip) {
Long iplong = null;
try{
final String[] ipNums = ip.split("\\.");
iplong = (Long.parseLong(ipNums[0]) << 24)
+ (Long.parseLong(ipNums[1]) << 16)
+ (Long.parseLong(ipNums[2]) << 8)
+ (Long.parseLong(ipNums[3]));
}catch(Exception e){
e.printStackTrace();
}
return iplong;
}
/**
* 将long ip地址转为字符串IP
* long型IP地址
* @param ipaddress
* @return
*/
public static String ipLong2Ip(Long ipaddress) {
if(ipaddress == null){
return null;
}
StringBuffer sb = new StringBuffer("");
sb.append(String.valueOf((ipaddress >>> 24)));
sb.append(".");
sb.append(String.valueOf((ipaddress & 0x00FFFFFF) >>> 16));
sb.append(".");
sb.append(String.valueOf((ipaddress & 0x0000FFFF) >>> 8));
sb.append(".");
sb.append(String.valueOf((ipaddress & 0x000000FF)));
return sb.toString();
}
IP地址和long互转
最新推荐文章于 2023-04-12 13:24:12 发布