Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.