转化代码
public static long StringToLong(String ipInString) {
ipInString = ipInString.replace(" ", "");
byte[] bytes;
if (ipInString.contains(":"))
bytes = ipv6ToBytes(ipInString);
else
bytes = ipv4ToBytes(ipInString);
BigInteger bigInt = new BigInteger(bytes);
return bigInt.longValue();
}
public static String StringToBigIntString(String ipInString) {
ipInString = ipInString.replace(" ", "");
byte[] bytes;
if (ipInString.contains(":"))
bytes = ipv6ToBytes(ipInString);
else
bytes = ipv4ToBytes(ipInString);
BigInteger bigInt = new BigInteger(bytes);
return bigInt.toString();
}
public static String BigIntToString(BigInteger ipInBigInt) {
byte[] bytes = ipInBigInt.toByteArray();
try {
String ip;
if (bytes.length > 4) {
ip = InetAddress.getByAddress(Arrays.copyOfRange(bytes, 1, bytes.length)).toString();
} else {
ip = InetAddress.getByAddress(bytes).toString();
}
return ip.substring(ip.indexOf('/') + 1).trim();
} catch (UnknownHostException e) {
throw new RuntimeException("错误的IP:" + ipInBigInt + "error is" + e);
}
}
public static int isIpV4OrV6(String ipAdress) throws Exception {
InetAddress address = InetAddress.getByName(ipAdress);
if (address instanceof Inet4Address)
return 4;
else if (address instanceof Inet6Address)
return 6;
return 0;
}
private static byte[] ipv4ToBytes(String ipv4) {
byte[] ret = new byte[5];
ret[0] = 0;
int position1 = ipv4.indexOf(".");
int position2 = ipv4.indexOf(".", position1 + 1);
int position3 = ipv4.indexOf(".", position2 + 1);
ret[1] = (byte) Integer.parseInt(ipv4.substring(0, position1));
ret[2] = (byte) Integer.parseInt(ipv4.substring(position1 + 1,
position2));
ret[3] = (byte) Integer.parseInt(ipv4.substring(position2 + 1,
position3));
ret[4] = (byte) Integer.parseInt(ipv4.substring(position3 + 1));
return ret;
}
public static boolean ipExistsInRange(String ip, String ipSection) {
ipSection = ipSection.trim();
ip = ip.trim();
int idx = ipSection.indexOf('-');
String beginIP = ipSection.substring(0, idx);
String endIP = ipSection.substring(idx + 1);
return getIp2long(beginIP) <= getIp2long(ip)
&& getIp2long(ip) <= getIp2long(endIP);
}
private static byte[] ipv6ToBytes(String ipv6) {
byte[] ret = new byte[17];
ret[0] = 0;
int ib = 16;
boolean comFlag = false;
if (ipv6.startsWith(":"))
ipv6 = ipv6.substring(1);
String groups[] = ipv6.split(":");
for (int ig = groups.length - 1; ig > -1; ig--) {
if (groups[ig].contains(".")) {
byte[] temp = ipv4ToBytes(groups[ig]);
ret[ib--] = temp[4];
ret[ib--] = temp[3];
ret[ib--] = temp[2];
ret[ib--] = temp[1];
comFlag = true;
} else if ("".equals(groups[ig])) {
int zlg = 9 - (groups.length + (comFlag ? 1 : 0));
while (zlg-- > 0) {
ret[ib--] = 0;
ret[ib--] = 0;
}
} else {
int temp = Integer.parseInt(groups[ig], 16);
ret[ib--] = (byte) temp;
ret[ib--] = (byte) (temp >> 8);
}
}
return ret;
}
public static long getIp2long(String ip) {
ip = ip.trim();
String[] ips = ip.split("\\.");
long ip2long = 0L;
for (int i = 0; i < 4; ++i) {
ip2long = ip2long << 8 | Integer.parseInt(ips[i]);
}
return ip2long;
}
public static long getIp2long2(String ip) {
ip = ip.trim();
String[] ips = ip.split("\\.");
long ip1 = Integer.parseInt(ips[0]);
long ip2 = Integer.parseInt(ips[1]);
long ip3 = Integer.parseInt(ips[2]);
long ip4 = Integer.parseInt(ips[3]);
long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256
+ ip4;
return ip2long;
}