JAVA中IP和整數(shù)相互轉(zhuǎn)化的方法
本文實(shí)例講述了JAVA中IP和整數(shù)相互轉(zhuǎn)化的方法。分享給大家供大家參考。具體分析如下:
一、基本知識點(diǎn)
IP ——> 整數(shù):
把IP地址轉(zhuǎn)化為字節(jié)數(shù)組
通過左移位(<<)、與(&)、或(|)這些操作轉(zhuǎn)為int
整數(shù) ——> IP:
將整數(shù)值進(jìn)行右移位操作(>>>),右移24位,再進(jìn)行與操作符(&)0xFF,得到的數(shù)字即為第一段IP。
將整數(shù)值進(jìn)行右移位操作(>>>),右移16位,再進(jìn)行與操作符(&)0xFF,得到的數(shù)字即為第二段IP。
將整數(shù)值進(jìn)行右移位操作(>>>),右移8位,再進(jìn)行與操作符(&)0xFF,得到的數(shù)字即為第三段IP。
將整數(shù)值進(jìn)行與操作符(&)0xFF,得到的數(shù)字即為第四段IP。
二、java代碼示例(IPv4Util.java)
package michael.utils; import java.net.InetAddress; public class IPv4Util { private final static int INADDRSZ = 4; public static byte[] ipToBytesByInet(String ipAddr) { try { return InetAddress.getByName(ipAddr).getAddress(); } catch (Exception e) { throw new IllegalArgumentException(ipAddr + " is invalid IP"); } }JTA實(shí)踐:Spring+ATOMIKOS public static byte[] ipToBytesByReg(String ipAddr) { byte[] ret = new byte[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); return ret; } catch (Exception e) { throw new IllegalArgumentException(ipAddr + " is invalid IP"); } } public static String bytesToIp(byte[] bytes) { return new StringBuffer().append(bytes[0] & 0xFF).append('.').append( bytes[1] & 0xFF).append('.').append(bytes[2] & 0xFF) .append('.').append(bytes[3] & 0xFF).toString(); } public static int bytesToInt(byte[] bytes) { int addr = bytes[3] & 0xFF; addr |= ((bytes[2] << 8) & 0xFF00); addr |= ((bytes[1] << 16) & 0xFF0000); addr |= ((bytes[0] << 24) & 0xFF000000); return addr; } public static int ipToInt(String ipAddr) { try { return bytesToInt(ipToBytesByInet(ipAddr)); } catch (Exception e) { throw new IllegalArgumentException(ipAddr + " is invalid IP"); } } public static byte[] intToBytes(int ipInt) { byte[] ipAddr = new byte[INADDRSZ]; ipAddr[0] = (byte) ((ipInt >>> 24) & 0xFF); ipAddr[1] = (byte) ((ipInt >>> 16) & 0xFF); ipAddr[2] = (byte) ((ipInt >>> 8) & 0xFF); ipAddr[3] = (byte) (ipInt & 0xFF); return ipAddr; } public static String intToIp(int ipInt) { return new StringBuilder().append(((ipInt >> 24) & 0xff)).append('.') .append((ipInt >> 16) & 0xff).append('.').append( (ipInt >> 8) & 0xff).append('.').append((ipInt & 0xff)) .toString(); } public static int[] getIPIntScope(String ipAndMask) { String[] ipArr = ipAndMask.split("/"); if (ipArr.length != 2) { throw new IllegalArgumentException("invalid ipAndMask with: " + ipAndMask); } int netMask = Integer.valueOf(ipArr[1].trim()); if (netMask < 0 || netMask > 31) { throw new IllegalArgumentException("invalid ipAndMask with: " + ipAndMask); } int ipInt = IPv4Util.ipToInt(ipArr[0]); int netIP = ipInt & (0xFFFFFFFF << (32 - netMask)); int hostScope = (0xFFFFFFFF >>> netMask); return new int[] { netIP, netIP + hostScope }; } public static String[] getIPAddrScope(String ipAndMask) { int[] ipIntArr = IPv4Util.getIPIntScope(ipAndMask); return new String[] { IPv4Util.intToIp(ipIntArr[0]), IPv4Util.intToIp(ipIntArr[0]) }; } public static int[] getIPIntScope(String ipAddr, String mask) { int ipInt; int netMaskInt = 0, ipcount = 0; try { ipInt = IPv4Util.ipToInt(ipAddr); if (null == mask || "".equals(mask)) { return new int[] { ipInt, ipInt }; } netMaskInt = IPv4Util.ipToInt(mask); ipcount = IPv4Util.ipToInt("255.255.255.255") - netMaskInt; int netIP = ipInt & netMaskInt; int hostScope = netIP + ipcount; return new int[] { netIP, hostScope }; } catch (Exception e) { throw new IllegalArgumentException("invalid ip scope express ip:" + ipAddr + " mask:" + mask); } } public static String[] getIPStrScope(String ipAddr, String mask) { int[] ipIntArr = IPv4Util.getIPIntScope(ipAddr, mask); return new String[] { IPv4Util.intToIp(ipIntArr[0]), IPv4Util.intToIp(ipIntArr[0]) }; } public static void main(String[] args) throws Exception { String ipAddr = "192.168.8.1"; byte[] bytearr = IPv4Util.ipToBytesByInet(ipAddr); StringBuffer byteStr = new StringBuffer(); for (byte b : 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 = new StringBuffer(); for (byte b : 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)); int ipInt = IPv4Util.ipToInt(ipAddr); System.out.println("IP: " + ipAddr + " --> int: " + ipInt); System.out.println("int: " + ipInt + " --> IP: " + IPv4Util.intToIp(ipInt)); String ipAndMask = "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]) + " ]"); String ipAddr1 = "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程序設(shè)計(jì)有所幫助。
- Java求兩個(gè)正整數(shù)的最大公約數(shù)和最小公倍數(shù)
- java中字符串轉(zhuǎn)整數(shù)及MyAtoi方法的實(shí)現(xiàn)
- Java根據(jù)正整數(shù)的位數(shù)并逆序打印出各位數(shù)字
- Java輸入三個(gè)整數(shù)并把他們由小到大輸出(x,y,z)
- Java將一個(gè)正整數(shù)分解質(zhì)因數(shù)的代碼
- 詳解Java判斷是否是整數(shù),小數(shù)或?qū)崝?shù)的正則表達(dá)式
- java實(shí)現(xiàn)整數(shù)轉(zhuǎn)化為中文大寫金額的方法
- 談?wù)凧ava中整數(shù)類型(short int long)的存儲方式
- java整數(shù)(秒數(shù))轉(zhuǎn)換為時(shí)分秒格式的示例
- Java得到一個(gè)整數(shù)的絕對值,不使用任何判斷和比較語句,包括API
- java中最大的整數(shù)用法分析
相關(guān)文章
Java多線程的實(shí)現(xiàn)方式比較(兩種方式比較)
Java多線程實(shí)現(xiàn)方式有兩種,第一種是繼承Thread類,第二種是實(shí)現(xiàn)Runnable接口,兩種有很多差異,下面跟著本文一起學(xué)習(xí)吧2015-11-11Java后端Cookie實(shí)現(xiàn)(時(shí)間戳)代碼實(shí)例
這篇文章主要介紹了Java后端Cookie實(shí)現(xiàn)(時(shí)間戳)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12利用feign調(diào)用返回object類型轉(zhuǎn)換成實(shí)體
這篇文章主要介紹了利用feign調(diào)用返回object類型轉(zhuǎn)換成實(shí)體,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Mybatis中使用updateBatch進(jìn)行批量更新
這篇文章主要介紹了Mybatis中使用updateBatch進(jìn)行批量更新的相關(guān)資料,有逐條更新,sql批量更新等,具體實(shí)例代碼大家參考下本文2018-04-04Springboot整合mybatis開啟二級緩存的實(shí)現(xiàn)示例
在一級緩存中,是查詢兩次數(shù)據(jù)庫的,顯然這是一種浪費(fèi),既然SQL查詢相同,就沒有必要再次查庫了,直接利用緩存數(shù)據(jù)即可,這種思想就是MyBatis二級緩存的初衷,本文就詳細(xì)的介紹了Springboot整合mybatis開啟二級緩存,感興趣的可以了解一下2022-05-05JAVA中字符串函數(shù)subString的用法小結(jié)
本篇文章主要是對JAVA中字符串函數(shù)subString的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼
這篇文章主要介紹了Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10SpringBoot 注解事務(wù)聲明式事務(wù)的方式
springboot使用上述注解的幾種方式開啟事物,可以達(dá)到和xml中聲明的同樣效果,但是卻告別了xml,使你的代碼遠(yuǎn)離配置文件。今天就扒一扒springboot中事務(wù)使用注解的玩法,感興趣的朋友一起看看吧2017-09-09C語言中下標(biāo)與指針的轉(zhuǎn)換以及指向指針的指針的例子
這篇文章主要介紹了C語言中下標(biāo)與指針的轉(zhuǎn)換以及指向指針的指針的示例,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-11-11