Last active
December 19, 2015 21:39
-
-
Save sitz/6021961 to your computer and use it in GitHub Desktop.
IPv4 Converter Util
This file contains hidden or 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
/** | |
* Created with IntelliJ IDEA. | |
* User: sitesh | |
* Date: 7/17/13 | |
* Time: 9:24 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class IPv4Converter { | |
private static String longToIp(int ip) { | |
int[] ipParts = new int[4]; | |
ipParts[0] = 0xFF & (ip >> 24); | |
ipParts[1] = 0xFF & (ip >> 16); | |
ipParts[2] = 0xFF & (ip >> 8); | |
ipParts[3] = 0xFF & (ip >> 0); | |
return ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + "." + ipParts[3]; | |
} | |
private static int ipToLong(String ipAddress) { | |
String[] ipParts = ipAddress.split("\\."); | |
int part0 = Integer.parseInt(ipParts[0]) << 24; | |
int part1 = Integer.parseInt(ipParts[1]) << 16; | |
int part2 = Integer.parseInt(ipParts[2]) << 8; | |
int part3 = Integer.parseInt(ipParts[3]) << 0; | |
return part0 + part1 + part2 + part3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment