I. Basic knowledge points
IP --> INTEGER:
? 6? 1. convert an IP address to a byte array
? 6? 1. convert it to int through the left shift (<), and (&), or (|) Operations
Integer --> IP:
? 6? 1. Shift the integer to the right (>>>) and shift it to the right by 24 digits. Then, with the operator (&) 0xFF, the obtained number is the first IP address.
? 6? 1. Shift the integer to the right (>>>), shift the integer to the right by 16 bits, and then start with the operator (&) 0xFF. The obtained number is the second segment of the IP address.
? 6? 1. Shift the integer to the right (>>>) and shift the integer to the right by 8 digits. Then, with the operator (&) 0xFF, the obtained number is the third segment of the IP address.
? 6? 1. Compare the integer with the operator (&) 0xFF to obtain the fourth IP address.
Ii. java code example
IPv4Util. java
Package michael. utils;
Import java.net. InetAddress;
/**
* Used for mutual conversion between IP addresses and Integers
* @ Author Andy. Wang
*
*/
Public class implements 4util {
Private final static int INADDRSZ = 4;
/**
* Convert an IP address to a byte array
* @ Param ipAddr
* @ Return byte []
*/
Public static byte [] ipToBytesByInet (String ipAddr ){
Try {
Return InetAddress. getByName (ipAddr). getAddress ();
} Catch (Exception e ){
Throw new IllegalArgumentException (ipAddr + "is invalid IP ");
}
}
/**
* Convert an IP address to an int.
* @ Param ipAddr
* @ Return int
*/
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 ");
}
}
/**
* Converting byte arrays to IP addresses
* @ Param bytes
* @ Return int
*/
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 ();
}
/**
* Use bitwise operations to convert byte []-> int
* @ Param bytes
* @ Return int
*/
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;
}
/**
* Convert an IP address to an int.
* @ Param ipAddr
* @ Return int
*/
Public static int ipToInt (String ipAddr ){
Try {
Return bytesToInt (ipToBytesByInet (ipAddr ));
} Catch (Exception e ){
Throw new IllegalArgumentException (ipAddr + "is invalid IP ");
}
}
/**
* IpInt-> byte []
* @ Param ipInt
* @ Return byte []
*/
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;
}
/**
* Set int-> IP Address
* @ Param ipInt
* @ Return String
*/
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 ();
}
/**
* Convert 192.168.1.1/24 to the int array range.
* @ Param ipAndMask
* @ Return int []
*/
Public static int [] getIPIntScope (String ipAndMask ){
String [] ipArr = ipAndMask. split ("/");
If (ipArr. length! = 2 ){
Throw new IllegalArgumentException ("invalid ipAndMask :"
+ IpAndMask );
}
Int netMask = Integer. valueOf (ipArr [1]. trim ());
If (netMask <0 | netMask> 31 ){
Throw new IllegalArgumentException ("invalid ipAndMask :"
+ IpAndMask );
}
Int ipInt = IPv4Util. ipToInt (ipArr [0]);
Int netIP = ipInt & (0 xFFFFFFFF <(32-netMask ));
Int hostScope = (0 xFFFFFFFF >>> netMask );
Return new int [] {netIP, netIP + hostScope };
}
/**
* Convert 192.168.1.1/24 to the IP array range.
* @ Param ipAndMask
* @ Return String []
*/
Public static String [] getIPAddrScope (String ipAndMask ){
Int [] ipIntArr = IPv4Util. getIPIntScope (ipAndMask );
Return new String [] {IPv4Util. intToIp (ipIntArr [0]),
Listen 4util. intToIp (ipIntArr [0])};
}
/**
* Convert the IP address Subnet Mask (192.168.1.1 255.255.255.0) to an IP address segment
* @ Param ipAddr
* @ Param mask
* @ Return int []
*/
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 );
}
}
/**
* Convert the IP address Subnet Mask (192.168.1.1 255.255.255.0) to an IP address segment
* @ Param ipAddr
* @ Param mask
* @ Return String []
*/
Public static String [] getIPStrScope (String ipAddr, String mask ){
Int [] ipIntArr = IPv4Util. getIPIntScope (ipAddr, mask );
Return new String [] {IPv4Util. intToIp (ipIntArr [0]),
Listen 4util. intToIp (ipIntArr [0])};
}
/**
* @ Param args
* @ Throws Exception
*/
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 address segment: [" + ipscope [0] + ","
+ Ipscope [1] + "]");
System. out. println (ipAndMask + "--> IP address segment :["
+ 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 address segment :["
+ Ipscope1 [0] + "," + ipscope1 [1] + "]");
System. out. println (ipAddr1 + "," + ipMask1 + "--> ip address segment :["
+ IPv4Util. intToIp (ipscope1 [0]) + ","
+ IPv4Util. intToIp (ipscope1 [1]) + "]");
}
}