//ip2longimport java.net.*;public class test {
public static int str2Ip(String ip) throws UnknownHostException {
InetAddress address = InetAddress.getByName(ip);
byte[] bytes = address.getAddress();
int a, b, c, d;
a = byte2int(bytes[0]);
b = byte2int(bytes[1]);
c = byte2int(bytes[2]);
d = byte2int(bytes[3]);
int result = (a << 24) | (b << 16) | (c << 8) | d;
return result;
}
public static int byte2int(byte b) {
int l = b & 0x07f;
if (b < 0) {
l |= 0x80;
}
return l;
}
public static long ip2long(String ip) throws UnknownHostException {
int ipNum = str2Ip(ip);
return int2long(ipNum);
} public static long int2long(int i) {
long l = i & 0x7fffffffL;
if (i < 0) {
l |= 0x080000000L;
}
return l;
} public static void main(String[] args) throws Exception {
System.out.println(ip2long("192.168.0.111"));
}}
public static int str2Ip(String ip) throws UnknownHostException {
InetAddress address = InetAddress.getByName(ip);
byte[] bytes = address.getAddress();
int a, b, c, d;
a = byte2int(bytes[0]);
b = byte2int(bytes[1]);
c = byte2int(bytes[2]);
d = byte2int(bytes[3]);
int result = (a << 24) | (b << 16) | (c << 8) | d;
return result;
}
public static int byte2int(byte b) {
int l = b & 0x07f;
if (b < 0) {
l |= 0x80;
}
return l;
}
public static long ip2long(String ip) throws UnknownHostException {
int ipNum = str2Ip(ip);
return int2long(ipNum);
} public static long int2long(int i) {
long l = i & 0x7fffffffL;
if (i < 0) {
l |= 0x080000000L;
}
return l;
} public static void main(String[] args) throws Exception {
System.out.println(ip2long("192.168.0.111"));
}}
解决方案 »
- ExtJs中tabpanel用了autoLoad加载一个页面之后,速度会很慢,怎么解决?
- spring采用Annotation实现AOP
- 如何删除 Google App Engine 里的一个application
- session.getAttribute()值转换问题
- 用struts打开只有边框的网页?
- 求教动态include页面并在被包含页面中共享变量的问题。
- 请问哪里可以下载JBUILDX啊?
- 请教:webapps\ROOT下有一目录test
- jsp中怎么运用 数据库的commit 和rollback
- 帮我看一看为什么?谢谢!我真的好烦?
- 在windows XP系统下如何安装jsp的运行环境
- Weblogic服务器,后台经常报IOException,求助
public static int str2Ip(String ip) throws UnknownHostException {
InetAddress address = InetAddress.getByName(ip);
byte[] bytes = address.getAddress();
int a, b, c, d;
a = byte2int(bytes[0]);
b = byte2int(bytes[1]);
c = byte2int(bytes[2]);
d = byte2int(bytes[3]);
int result = (a << 24) | (b << 16) | (c << 8) | d;
return result;
}
public static int byte2int(byte b) {
int l = b & 0x07f;
if (b < 0) {
l |= 0x80;
}
return l;
}
public static long ip2long(String ip) throws UnknownHostException {
int ipNum = str2Ip(ip);
return int2long(ipNum);
} public static long int2long(int i) {
long l = i & 0x7fffffffL;
if (i < 0) {
l |= 0x080000000L;
}
return l;
}
public static String long2ip(long ip) {
int [] b=new int[4] ;
b[0] = (int)((ip >> 24) & 0xff);
b[1] = (int)((ip >> 16) & 0xff);
b[2] = (int)((ip >> 8) & 0xff);
b[3] = (int)(ip & 0xff);
String x;
Integer p;
p=new Integer(0);
x=p.toString(b[0])+"."+p.toString(b[1])+"."+p.toString(b[2])+"."+p.toString(b[3]);
return x;
} public static void main(String[] args) throws Exception {
long ip=ip2long("192.168.0.111");
System.out.println(ip);
System.out.println(long2ip(ip));
}}