一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - JAVA中IP和整數相互轉化的方法

JAVA中IP和整數相互轉化的方法

2019-12-19 13:35wo_soul JAVA教程

這篇文章主要介紹了JAVA中IP和整數相互轉化的方法,涉及java數值轉換的相關技巧,需要的朋友可以參考下

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

一、基本知識點

IP ——> 整數:
把IP地址轉化為字節數組
通過左移位(<<)、與(&)、或(|)這些操作轉為int
整數 ——> IP:
將整數值進行右移位操作(>>>),右移24位,再進行與操作符(&)0xFF,得到的數字即為第一段IP。
將整數值進行右移位操作(>>>),右移16位,再進行與操作符(&)0xFF,得到的數字即為第二段IP。
將整數值進行右移位操作(>>>),右移8位,再進行與操作符(&)0xFF,得到的數字即為第三段IP。
將整數值進行與操作符(&)0xFF,得到的數字即為第四段IP。

二、java代碼示例(IPv4Util.java)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美特级特黄a大片免费 | 99久久国产视频 | 精品国产乱码久久久人妻 | 久久综合色超碰人人 | 单亲乱l仑在线观看免费观看 | 污到你怀疑人生 | 深夜免费在线视频 | 袖珍人与大黑人性视频 | 精品久久洲久久久久护士免费 | 日韩一区三区 | 无限资源在线观看完整版免费下载 | 精品夜夜澡人妻无码AV蜜桃 | 日韩精品福利视频一区二区三区 | 精品福利一区 | 男女车车好快的车车免费网站 | 国产乱码免费卡1卡二卡3卡四 | 国产成人在线小视频 | 精品国产在天天线在线麻豆 | 图片专区亚洲欧美另类 | 精品日韩二区三区精品视频 | 9热在线精品视频观看 | ssni-497新任美脚女教师 | 男人天堂网在线 | 三级伦理在线播放 | 色多多在线观看视频 | 色姑娘导航 | 国产高清一区二区三区免费视频 | 毛片段| 色橹| 亚洲精品久久久久福利网站 | 动漫白丝袜美女羞羞 | 国内精品久久久久影院网站 | 88av免费观看 | 秋霞午夜 | 男人女人插 | 欧美vpswindowssex 欧美va在线高清 | 国产精品嫩草影院在线看 | 果冻传媒在线视频观看免费 | 日产乱码卡一卡2卡三卡四福利 | 日本国产高清色www视频在线 | 精品91 |