How to convert the IP address represented by an integer to a string representation in Java

Source: Internet
Author: User
Tags final integer
/** 
 * Converts an integer representation of an IP address to a string representation. 
 * 
 * @param IP 32-bit integer representation of the IP address 
 * @return dot representation of the IP address * * 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 ();  
}
 

Author: csdn blog Micro WX Smile

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.