数值->点分字符串
private static String longToIP(long longIP) { StringBuffer sb=new StringBuffer(""); sb.append(String.valueOf(longIP&0x000000FF)); sb.append("."); sb.append(String.valueOf((longIP&0x0000FFFF)>>>8)); sb.append("."); sb.append(String.valueOf((longIP&0x00FFFFFF)>>>16)); sb.append("."); sb.append(String.valueOf(longIP>>>24));//直u25509 右u31227 24位 return sb.toString(); }
点分字符串->数值
private static long ipToLong(String strIP) { long [] ip=new long[4]; int position1=strIP.indexOf("."); int position2=strIP.indexOf(".",position1+1); int position3=strIP.indexOf(".",position2+1); ip[0]=Long.parseLong(strIP.substring(0,position1)); ip[1]=Long.parseLong(strIP.substring(position1+1,position2)); ip[2]=Long.parseLong(strIP.substring(position2+1,position3)); ip[3]=Long.parseLong(strIP.substring(position3+1)); return (ip[3]<<24)+(ip[2]<<16)+(ip[1]<<8)+ip[0]; }
登录为: 读者 (reader)