原文:java
http://www.mkyong.com/java/java-convert-ip-address-to-decimal-number/算法
來自同一個博客。bash
IP轉爲數字(第二種算法。用左移、按位或實現。效率比上一篇的更高。):app
[java] view plaincopyoop
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">ui
public long ipToLong(String ipAddress) { spa
long result = 0; .net
String[] ipAddressInArray = ipAddress.split("\\."); code
for (int i = 3; i >= 0; i--) { blog
long ip = Long.parseLong(ipAddressInArray[3 - i]);
//left shifting 24,16,8,0 and bitwise OR
//1. 192 << 24
//1. 168 << 16
//1. 1 << 8
//1. 2 << 0
result |= ip << (i * 8);
}
return result;
}
數字轉爲IP,兩種算法都差很少:
[java] view plaincopy
<EMBED id=ZeroClipboardMovie_2 height=18 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
//ip = 3232235778
public String longToIp(long ip) {
StringBuilder result = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
result.insert(0,Long.toString(ip & 0xff));
if (i < 3) {
result.insert(0,'.');
}
ip = ip >> 8;
}
return result.toString();
}
//ip = 3232235778
public String longToIp2(long ip) {
return ((ip >> 24) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "."
+ ((ip >> 8) & 0xFF) + "."
+ (ip & 0xFF);
}
完整代碼:
[java] view plaincopy
<EMBED id=ZeroClipboardMovie_3 height=18 name=ZeroClipboardMovie_3 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=3&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
public class JavaBitwiseExample {
public static void main(String[] args) {
JavaBitwiseExample obj = new JavaBitwiseExample();
System.out.println("iptoLong : " + obj.ipToLong("192.168.1.2"));
System.out.println("iptoLong2 : " + obj.ipToLong2("192.168.1.2"));
System.out.println("longToIp : " + obj.longToIp(3232235778L));
System.out.println("longToIp2 : " + obj.longToIp2(3232235778L));
}
// example : 192.168.1.2
public long ipToLong(String ipAddress) {
// ipAddressInArray[0] = 192
String[] ipAddressInArray = ipAddress.split("\\.");
long result = 0;
for (int i = 0; i < ipAddressInArray.length; i++) {
int power = 3 - i;
int ip = Integer.parseInt(ipAddressInArray[i]);
// 1. 192 * 256^3
// 2. 168 * 256^2
// 3. 1 * 256^1
// 4. 2 * 256^0
result += ip * Math.pow(256, power);
}
return result;
}
public long ipToLong2(String ipAddress) {
long result = 0;
String[] ipAddressInArray = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(ipAddressInArray[3 - i]);
// left shifting 24,16,8,0 and bitwise OR
// 1. 192 << 24
// 1. 168 << 16
// 1. 1 << 8
// 1. 2 << 0
result |= ip << (i * 8);
}
return result;
}
public String longToIp(long i) {
return ((i >> 24) & 0xFF) +
"." + ((i >> 16) & 0xFF) +
"." + ((i >> 8) & 0xFF) +
"." + (i & 0xFF);
}
public String longToIp2(long ip) {
StringBuilder sb = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
// 1. 2
// 2. 1
// 3. 168
// 4. 192
sb.insert(0, Long.toString(ip & 0xff));
if (i < 3) {
sb.insert(0, '.');
}
// 1. 192.168.1.2
// 2. 192.168.1
// 3. 192.168
// 4. 192
ip = ip >> 8;
}
return sb.toString();
}
}
Output:
iptoLong : 3232235778iptoLong2 : 3232235778longToIp : 192.168.1.2 longToIp2 : 192.168.1.2