最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA中IP和整數(shù)相互轉化的方法

 更新時間:2015年05月29日 15:06:42   作者:wo_soul  
這篇文章主要介紹了JAVA中IP和整數(shù)相互轉化的方法,涉及java數(shù)值轉換的相關技巧,需要的朋友可以參考下

本文實例講述了JAVA中IP和整數(shù)相互轉化的方法。分享給大家供大家參考。具體分析如下:

一、基本知識點

IP ——> 整數(shù):
把IP地址轉化為字節(jié)數(shù)組
通過左移位(<<)、與(&)、或(|)這些操作轉為int
整數(shù) ——> IP:
將整數(shù)值進行右移位操作(>>>),右移24位,再進行與操作符(&)0xFF,得到的數(shù)字即為第一段IP。
將整數(shù)值進行右移位操作(>>>),右移16位,再進行與操作符(&)0xFF,得到的數(shù)字即為第二段IP。
將整數(shù)值進行右移位操作(>>>),右移8位,再進行與操作符(&)0xFF,得到的數(shù)字即為第三段IP。
將整數(shù)值進行與操作符(&)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實踐: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程序設計有所幫助。

相關文章

  • Java后臺批量生產(chǎn)echarts圖表并保存圖片

    Java后臺批量生產(chǎn)echarts圖表并保存圖片

    這篇文章主要介紹了Java后臺批量生產(chǎn)echarts圖表并保存圖片,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Java的優(yōu)先隊列PriorityQueue原理及實例分析

    Java的優(yōu)先隊列PriorityQueue原理及實例分析

    這篇文章主要介紹了Java的優(yōu)先隊列PriorityQueue原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 迅速學會@ConfigurationProperties的使用操作

    迅速學會@ConfigurationProperties的使用操作

    這篇文章主要介紹了迅速學會@ConfigurationProperties的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中你真的會用Constructor構造器嗎之看完本篇你就真的會了

    Java中你真的會用Constructor構造器嗎之看完本篇你就真的會了

    顯式初始化要求我們在寫程序時就確定初始值,這有時很不方便。我們可以使用構造器(constructor)來初始化對象。構造器可以初始化數(shù)據(jù)成員,還可以規(guī)定特定的操作。這些操作會在創(chuàng)建對象時自動執(zhí)行。下面文字將對該內容做詳細介紹,需要的小伙伴請參考
    2021-09-09
  • SpringBoot3集成Quartz的示例代碼

    SpringBoot3集成Quartz的示例代碼

    Quartz由Java編寫的功能豐富的開源作業(yè)調度框架,可以集成到幾乎任何Java應用程序中,并且能夠創(chuàng)建多個作業(yè)調度,在實際的業(yè)務中,有很多場景依賴定時任務,比如常見的:訂單超時處理,業(yè)務識別和預警通知等,本文介紹了SpringBoot3如何集成Quartz
    2023-08-08
  • 淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用

    淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用

    這篇文章主要介紹了淺談SpringBoot之開啟數(shù)據(jù)庫遷移的FlyWay使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 淺談java并發(fā)之計數(shù)器CountDownLatch

    淺談java并發(fā)之計數(shù)器CountDownLatch

    CountDownLatch是通過一個計數(shù)器來實現(xiàn)的,當我們在new 一個CountDownLatch對象的時候需要帶入該計數(shù)器值,該值就表示了線程的數(shù)量。下面我們來深入了解一下吧
    2019-06-06
  • SpringBoot實現(xiàn)圖片防盜鏈功能

    SpringBoot實現(xiàn)圖片防盜鏈功能

    出于安全考慮,我們需要后端返回的圖片只允許在某個網(wǎng)站內展示,不想被爬蟲拿到圖片地址后被下載,或者,不想瀏覽器直接訪問圖片鏈接,所以本文將給大家介紹SpringBoot實現(xiàn)圖片防盜鏈功能,需要的朋友可以參考下
    2024-04-04
  • SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解

    SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解

    這篇文章主要想和大家聊聊kaptcha的用法,畢竟這個已經(jīng)有16年歷史的玩意還在有人用,說明它的功能還是相當強大的,感興趣的小伙伴可以了解一下
    2022-05-05
  • 使用spring的restTemplate注意點

    使用spring的restTemplate注意點

    這篇文章主要介紹了使用spring的restTemplate注意點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論

五原县| 呼伦贝尔市| 沙湾县| 龙门县| 武清区| 遵化市| 车险| 拉萨市| 乌审旗| 五峰| 石泉县| 尖扎县| 拜城县| 竹溪县| 南安市| 章丘市| 蒙城县| 宁都县| 高碑店市| 论坛| 纳雍县| 德保县| 肥东县| 霸州市| 揭东县| 北京市| 沂水县| 油尖旺区| 竹溪县| 玛沁县| 平湖市| 毕节市| 交口县| 石首市| 威宁| 曲阜市| 仲巴县| 安龙县| 湟中县| 牙克石市| 萍乡市|