当前位置:学者斋 >

计算机 >java语言 >

JAVA中IP和整数相互转化的方法

JAVA中IP和整数相互转化的方法

JAVA中IP和整数如何相互转化你知道吗?你对JAVA中IP和整数相互转化的方法了解吗?下面是小编为大家带来的JAVA中IP和整数相互转化的知识,欢迎阅读。

JAVA中IP和整数相互转化的方法

  一、基本知识点

IP ——> 整数:

把IP地址转化为字节数组

通过左移位(<<)、与(&)、或(|)这些操作转为int

整数 ——> IP:

将整数值进行右移位操作(>>>),右移24位,再进行与操作符(&)0xFF,得到的数字即为第一段IP。

将整数值进行右移位操作(>>>),右移16位,再进行与操作符(&)0xFF,得到的'数字即为第二段IP。

将整数值进行右移位操作(>>>),右移8位,再进行与操作符(&)0xFF,得到的数字即为第三段IP。

将整数值进行与操作符(&)0xFF,得到的数字即为第四段IP。

  二、java代码示例()

package s;

import Address;

public class IPv4Util {

private final static int INADDRSZ = 4;

public static byte[] ipToBytesByInet(String ipAddr) {

try {

return yName(ipAddr)ddress();

} 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 = t(".");

ret[0] = (byte) (eInt(ipArr[0]) & 0xFF);

ret[1] = (byte) (eInt(ipArr[1]) & 0xFF);

ret[2] = (byte) (eInt(ipArr[2]) & 0xFF);

ret[3] = (byte) (eInt(ipArr[3]) & 0xFF);

return ret;

} catch (Exception e) {

throw new IllegalArgumentException(ipAddr + " is invalid IP");

}

}

public static String bytesToIp(byte[] bytes) {

return new StringBuffer()nd(bytes[0] & 0xFF)nd('.')nd(

bytes[1] & 0xFF)nd('.')nd(bytes[2] & 0xFF)

nd('.')nd(bytes[3] & 0xFF)ring();

}

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()nd(((ipInt >> 24) & 0xff))nd('.')

nd((ipInt >> 16) & 0xff)nd('.')nd(

(ipInt >> 8) & 0xff)nd('.')nd((ipInt & 0xff))

ring();

}

public static int[] getIPIntScope(String ipAndMask) {

String[] ipArr = t("/");

if (th != 2) {

throw new IllegalArgumentException("invalid ipAndMask with: "

+ ipAndMask);

}

int netMask = eOf(ipArr[1]());

if (netMask < 0 || netMask > 31) {

throw new IllegalArgumentException("invalid ipAndMask with: "

+ ipAndMask);

}

int ipInt = Int(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 = PIntScope(ipAndMask);

return new String[] { oIp(ipIntArr[0]),

oIp(ipIntArr[0]) };

}

public static int[] getIPIntScope(String ipAddr, String mask) {

int ipInt;

int netMaskInt = 0, ipcount = 0;

try {

ipInt = Int(ipAddr);

if (null == mask || ""ls(mask)) {

return new int[] { ipInt, ipInt };

}

netMaskInt = Int(mask);

ipcount = Int("") - 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 = PIntScope(ipAddr, mask);

return new String[] { oIp(ipIntArr[0]),

oIp(ipIntArr[0]) };

}

public static void main(String[] args) throws Exception {

String ipAddr = "";

byte[] bytearr = BytesByInet(ipAddr);

StringBuffer byteStr = new StringBuffer();

for (byte b : bytearr) {

if (th() == 0) {

nd(b);

} else {

nd("," + b);

}

}

tln("IP: " + ipAddr + " ByInet --> byte[]: [ " + byteStr

+ " ]");

bytearr = BytesByReg(ipAddr);

byteStr = new StringBuffer();

for (byte b : bytearr) {

if (th() == 0) {

nd(b);

} else {

nd("," + b);

}

}

tln("IP: " + ipAddr + " ByReg --> byte[]: [ " + byteStr

+ " ]");

tln("byte[]: " + byteStr + " --> IP: "

+ sToIp(bytearr));

int ipInt = Int(ipAddr);

tln("IP: " + ipAddr + " --> int: " + ipInt);

tln("int: " + ipInt + " --> IP: "

+ oIp(ipInt));

String ipAndMask = "";

int[] ipscope = PIntScope(ipAndMask);

tln(ipAndMask + " --> int地址段:[ " + ipscope[0] + ","

+ ipscope[1] + " ]");

tln(ipAndMask + " --> IP 地址段:[ "

+ oIp(ipscope[0]) + ","

+ oIp(ipscope[1]) + " ]");

String ipAddr1 = "", ipMask1 = "";

int[] ipscope1 = PIntScope(ipAddr1, ipMask1);

tln(ipAddr1 + " , " + ipMask1 + " --> int地址段 :[ "

+ ipscope1[0] + "," + ipscope1[1] + " ]");

tln(ipAddr1 + " , " + ipMask1 + " --> IP地址段 :[ "

+ oIp(ipscope1[0]) + ","

+ oIp(ipscope1[1]) + " ]");

}

}

希望本文所述对大家的java程序设计有所帮助。

标签: JAVA IP 整数 转化
  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/java/2xje9d.html