How to convert an integer representation of an IP address to a string representation in Java

Source: Internet
Author: User

/**
* Converts an integer representation of an IP address to a string representation.
*
* IP Address @param IP 32-bit integer
* @return The IP address represented by the fractional point
*/
public static final String Long2ip (final long IP) {
Final long[] mask = {0x000000ff, 0x0000FF00, 0x00ff0000, 0xff000000};
Final StringBuilder ipAddress = new StringBuilder ();
for (int i = 0; i < mask.length; i++) {
Ipaddress.insert (0, (IP & mask[i]) >> (i * 8));
if (I < mask.length-1) {
Ipaddress.insert (0, ".");
}
}
return ipaddress.tostring ();
}

BYTES[2] = (byte) ((V & 0x0000ff00) >>> 8);

The type V int is a 32-bit 0x0000ff00 is also 32 bits (0000 0000 1111 1111 0000 0000)
The function of the v&0x0000ff00 is to shield the last V 8 bits (into 0) and then move the unsigned 8-bit to the right, which is the so-called high 8-bit.
Similarly v & 0x000000ff); The function is to get eighth place.

How to convert an integer representation of an IP address to a string representation in Java

Related Article

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.