convert int ip value to dotted ip address format - Java Network

Java examples for Network:IP Address

Description

convert int ip value to dotted ip address format

Demo Code

/** /*from   w ww .j  a v  a  2 s .  c o m*/
 * Copyright 2012 InCNTRE, This file is released under Apache 2.0 license except for component libraries under different licenses
http://www.apache.org/licenses/LICENSE-2.0
 */

public class Main{

    /**
     * convert int ip value to dotted ip address format 
     * @param ipValue
     * @return
     */
    public static String toIPString(int ipValue) {

        FlowscaleController.logger.info("ip address is {}", ipValue);

        String a = Integer.toBinaryString(ipValue);
        String zeroString = "";
        for (int i = a.length(); i < 32; i++) {
            zeroString += "0";
        }
        a = zeroString + a;

        String ip = "";
        for (int i = 0; i < 4; i++) {

            ip = ip + "."
                    + Integer.parseInt(a.substring(i * 8, 8 * (i + 1)), 2);

        }

        return ip.substring(1);

    }
}

Related Tutorials