Android How to - Convert Int value to IP address








Question

We would like to know how to convert Int value to IP address.

Answer

public class Main {
  public static void main(String[] argv) {
    System.out.println(ipv4Int2Str(12333212));
  }//  w  w  w. j ava2 s . co m

  static public String ipv4Int2Str(int addr) {
    StringBuffer ipBuf = new StringBuffer();
    ipBuf.append(addr & 0xff).append('.').append((addr >>>= 8) & 0xff)
        .append('.').append((addr >>>= 8) & 0xff).append('.')
        .append((addr >>>= 8) & 0xff);

    return ipBuf.toString();
  }
}