JAVA中IP和整数相互转化的方法
本文实例讲述了JAVA中IP和整数相互转化的方法。分享给大家供大家参考。具体分析如下:
一、基本知识点
IP——>整数:
把IP地址转化为字节数组
通过左移位(<<)、与(&)、或(|)这些操作转为int
整数——>IP:
将整数值进行右移位操作(>>>),右移24位,再进行与操作符(&)0xFF,得到的数字即为第一段IP。
将整数值进行右移位操作(>>>),右移16位,再进行与操作符(&)0xFF,得到的数字即为第二段IP。
将整数值进行右移位操作(>>>),右移8位,再进行与操作符(&)0xFF,得到的数字即为第三段IP。
将整数值进行与操作符(&)0xFF,得到的数字即为第四段IP。
二、java代码示例(IPv4Util.java)
packagemichael.utils; importjava.net.InetAddress; publicclassIPv4Util{ privatefinalstaticintINADDRSZ=4; publicstaticbyte[]ipToBytesByInet(StringipAddr){ try{ returnInetAddress.getByName(ipAddr).getAddress(); }catch(Exceptione){ thrownewIllegalArgumentException(ipAddr+"isinvalidIP"); } }JTA实践:Spring+ATOMIKOS publicstaticbyte[]ipToBytesByReg(StringipAddr){ byte[]ret=newbyte[4]; try{ String[]ipArr=ipAddr.split("\\."); ret[0]=(byte)(Integer.parseInt(ipArr[0])&0xFF); ret[1]=(byte)(Integer.parseInt(ipArr[1])&0xFF); ret[2]=(byte)(Integer.parseInt(ipArr[2])&0xFF); ret[3]=(byte)(Integer.parseInt(ipArr[3])&0xFF); returnret; }catch(Exceptione){ thrownewIllegalArgumentException(ipAddr+"isinvalidIP"); } } publicstaticStringbytesToIp(byte[]bytes){ returnnewStringBuffer().append(bytes[0]&0xFF).append('.').append( bytes[1]&0xFF).append('.').append(bytes[2]&0xFF) .append('.').append(bytes[3]&0xFF).toString(); } publicstaticintbytesToInt(byte[]bytes){ intaddr=bytes[3]&0xFF; addr|=((bytes[2]<<8)&0xFF00); addr|=((bytes[1]<<16)&0xFF0000); addr|=((bytes[0]<<24)&0xFF000000); returnaddr; } publicstaticintipToInt(StringipAddr){ try{ returnbytesToInt(ipToBytesByInet(ipAddr)); }catch(Exceptione){ thrownewIllegalArgumentException(ipAddr+"isinvalidIP"); } } publicstaticbyte[]intToBytes(intipInt){ byte[]ipAddr=newbyte[INADDRSZ]; ipAddr[0]=(byte)((ipInt>>>24)&0xFF); ipAddr[1]=(byte)((ipInt>>>16)&0xFF); ipAddr[2]=(byte)((ipInt>>>8)&0xFF); ipAddr[3]=(byte)(ipInt&0xFF); returnipAddr; } publicstaticStringintToIp(intipInt){ returnnewStringBuilder().append(((ipInt>>24)&0xff)).append('.') .append((ipInt>>16)&0xff).append('.').append( (ipInt>>8)&0xff).append('.').append((ipInt&0xff)) .toString(); } publicstaticint[]getIPIntScope(StringipAndMask){ String[]ipArr=ipAndMask.split("/"); if(ipArr.length!=2){ thrownewIllegalArgumentException("invalidipAndMaskwith:" +ipAndMask); } intnetMask=Integer.valueOf(ipArr[1].trim()); if(netMask<0||netMask>31){ thrownewIllegalArgumentException("invalidipAndMaskwith:" +ipAndMask); } intipInt=IPv4Util.ipToInt(ipArr[0]); intnetIP=ipInt&(0xFFFFFFFF<<(32-netMask)); inthostScope=(0xFFFFFFFF>>>netMask); returnnewint[]{netIP,netIP+hostScope}; } publicstaticString[]getIPAddrScope(StringipAndMask){ int[]ipIntArr=IPv4Util.getIPIntScope(ipAndMask); returnnewString[]{IPv4Util.intToIp(ipIntArr[0]), IPv4Util.intToIp(ipIntArr[0])}; } publicstaticint[]getIPIntScope(StringipAddr,Stringmask){ intipInt; intnetMaskInt=0,ipcount=0; try{ ipInt=IPv4Util.ipToInt(ipAddr); if(null==mask||"".equals(mask)){ returnnewint[]{ipInt,ipInt}; } netMaskInt=IPv4Util.ipToInt(mask); ipcount=IPv4Util.ipToInt("255.255.255.255")-netMaskInt; intnetIP=ipInt&netMaskInt; inthostScope=netIP+ipcount; returnnewint[]{netIP,hostScope}; }catch(Exceptione){ thrownewIllegalArgumentException("invalidipscopeexpressip:" +ipAddr+"mask:"+mask); } } publicstaticString[]getIPStrScope(StringipAddr,Stringmask){ int[]ipIntArr=IPv4Util.getIPIntScope(ipAddr,mask); returnnewString[]{IPv4Util.intToIp(ipIntArr[0]), IPv4Util.intToIp(ipIntArr[0])}; } publicstaticvoidmain(String[]args)throwsException{ StringipAddr="192.168.8.1"; byte[]bytearr=IPv4Util.ipToBytesByInet(ipAddr); StringBufferbyteStr=newStringBuffer(); for(byteb:bytearr){ if(byteStr.length()==0){ byteStr.append(b); }else{ byteStr.append(","+b); } } System.out.println("IP:"+ipAddr+"ByInet-->byte[]:["+byteStr +"]"); bytearr=IPv4Util.ipToBytesByReg(ipAddr); byteStr=newStringBuffer(); for(byteb:bytearr){ if(byteStr.length()==0){ byteStr.append(b); }else{ byteStr.append(","+b); } } System.out.println("IP:"+ipAddr+"ByReg-->byte[]:["+byteStr +"]"); System.out.println("byte[]:"+byteStr+"-->IP:" +IPv4Util.bytesToIp(bytearr)); intipInt=IPv4Util.ipToInt(ipAddr); System.out.println("IP:"+ipAddr+"-->int:"+ipInt); System.out.println("int:"+ipInt+"-->IP:" +IPv4Util.intToIp(ipInt)); StringipAndMask="192.168.1.1/24"; int[]ipscope=IPv4Util.getIPIntScope(ipAndMask); System.out.println(ipAndMask+"-->int地址段:["+ipscope[0]+"," +ipscope[1]+"]"); System.out.println(ipAndMask+"-->IP地址段:[" +IPv4Util.intToIp(ipscope[0])+"," +IPv4Util.intToIp(ipscope[1])+"]"); StringipAddr1="192.168.1.1",ipMask1="255.255.255.0"; int[]ipscope1=IPv4Util.getIPIntScope(ipAddr1,ipMask1); System.out.println(ipAddr1+","+ipMask1+"-->int地址段:[" +ipscope1[0]+","+ipscope1[1]+"]"); System.out.println(ipAddr1+","+ipMask1+"-->IP地址段:[" +IPv4Util.intToIp(ipscope1[0])+"," +IPv4Util.intToIp(ipscope1[1])+"]"); } }
希望本文所述对大家的java程序设计有所帮助。