将IP转化成10进制数,10进制数转化成IP

/** 
*IPUtil  
*getIp10,将xxx.xxx.xx.x类型的IP转换成10进制的long型串 
*/ 
public static long getIp10(String ip) {     
       long ip10 = 0;     
       String[] ss = ip.trim().split("\\.");     
       for (int i = 0; i < 4; i++) {     
           ip10 += Math.pow(256, 3 - i) * Integer.parseInt(ss[i]);     
       }     
   
       return ip10;     
   }  
 
/** 
*IPUtil  
*getIp,将long型串转换成xxx.xxx.xx.x型的IP 
*/ 
public static String getIp(long ip10) {     
        String ip = "";     
        long temp = 0;     
        for (int i = 3; i >= 0; i--) {     
            temp = ip10 / (long) Math.pow(256, i) % 256;     
            if (i == 3) {     
                ip = ip + temp;     
            } else {     
                ip = ip + "." + temp;     
            }     
        }     
        return ip;     

你可能感兴趣的:(IP)