******* 关注数值溢出问题
public class IPUtil {
public static long IPToLong(String ipAddress)
{
String[] ipqi_ary=ipAddress.split("\\.");
long ip0= new Long(ipqi_ary[0]).longValue();
long ip1= new Long(ipqi_ary[1]).longValue();
long ip2= new Long(ipqi_ary[2]).longValue();
long ip3= new Long(ipqi_ary[3]).longValue();
return (ip0 << 24) + (ip1 << 16) + (ip2 << 8) + ip3;
}
public static String LongToIP(long ipAddress)
{
long ui1 = ipAddress & 0xFF000000;
ui1 = ui1 >> 24;
long ui2 = ipAddress & 0x00FF0000;
ui2 = ui2 >> 16;
long ui3 = ipAddress & 0x0000FF00;
ui3 = ui3 >> 8;
long ui4 = ipAddress & 0x000000FF;
String IPstr = "";
IPstr = String.valueOf(ui1) + "." + String.valueOf(ui2) + "."
+ String.valueOf(ui3) + "." + String.valueOf(ui4);
return IPstr;
}
public static void main(String args[]){
//
String ipAddress="255.255.255.255";
long ipInt=IPUtil.IPToLong(ipAddress);
System.out.println("String to int :"+ipInt);
System.out.println("int to String :"+IPUtil.LongToIP(ipInt));
}
}
------君临天下,舍我其谁------