Created
July 6, 2014 15:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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