Skip to content

Instantly share code, notes, and snippets.

@mide42
Created July 6, 2014 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mide42/a19c53799423804940d4 to your computer and use it in GitHub Desktop.
Save mide42/a19c53799423804940d4 to your computer and use it in GitHub Desktop.
Method to convert IPv4 address contained in a single int-value into string
/**
* Get string with IPv4 address contained in a single
* int-value. This format for IP addresses is
* used by class {@link android.net.DhcpInfo}
* (public member variables, e.g. <i>gateway</i>
* or <i>dns1</i>).
*
* @param ipAsInt IP encoded as int-value
* @return String with IP address, for example <i>127.0.0.1</i>
*/
public static String ipAsIntToStr(int ipAsInt) {
StringBuffer sb = new StringBuffer();
sb.append(ipAsInt & 0xFF);
sb.append(".");
sb.append((ipAsInt >>>= 8) & 0xFF);
sb.append(".");
sb.append((ipAsInt >>>= 8) & 0xFF);
sb.append(".");
sb.append((ipAsInt >>>= 8) & 0xFF);
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment