Introduction to converting IP addresses and integers in JAVA

Source: Internet
Author: User

The comparison function of IP (v4) ranges is often used in projects. Generally, IP addresses are converted to Integers before comparison.
I. Basic knowledge points


 

IP --> INTEGER:

Convert IP addresses to byte Arrays
Convert to int through the left shift (<), and (&), or (|) Operations
Integer --> IP:

Perform the right shift operation (>>>) on the integer, shift the integer to the right by 24 bits, and then perform the operation with the operator (&) 0xFF. The obtained number is the first IP address.
Perform the right shift operation (>>>) on the integer, shift 16 bits to the right, and then perform the operation with the operator (&) 0xFF. The resulting number is the second segment of the IP address.
Perform the right shift operation (>>>) on the integer, shift the integer to the right by 8 digits, and then perform the operation with the operator (&) 0xFF. The obtained number is the third segment of the IP address.
Perform the integer value and operator (&) 0xFF. The obtained number is the fourth IP address.
Ii. java code example

IPv4Util. java


 

Java code
Package michael. utils;

Import java.net. InetAddress;

/**
* @ Author michael <br>
* Blog: http://sjsky.iteye.com <br>
* Mail: sjsky007@gmail.com
*/
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 (
& N

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.