您好,我有一个数据库可以从中选择 IP 位置>
该脚本在 php 中,我正在将其转换为 java,但我不知道ip2long('127.0.0.1' ));
java中的等价物
基本上,这会将您的点 IP 地址字符串转换为长字符串。
public static Long Dot2LongIP(String dottedIP) {
String[] addrArray = dottedIP.split("\\.");
long num = 0;
for (int i=0;i<addrArray.length;i++) {
int power = 3-i;
num += ((Integer.parseInt(addrArray[i]) % 256) * Math.pow(256,power));
}
return num;
}
public static Long ipToLong(String stringIp) {
return Arrays.stream(stringIp.split("\\."))
.mapToLong(Long::parseLong)
.reduce(0,
(a, b) -> (a << 8) + b);
}
public static String ipToString(Long longIp){
return ((longIp >> 24) & 0xFF) + "." +
((longIp >> 16) & 0xFF) + "." +
((longIp >> 8) & 0xFF) + "." +
(longIp & 0xFF);
}
我认为在 Java 中没有标准的 API 可以做到这一点,但是
1/ InetAddress 类为您提供了一种获取字节数组的方法。
2/如果你真的需要一个整数,你可以使用这个片段,在http://www.myteneo.net/blog/-/blogs/java-ip-address-to-integer-and-back/
public static String intToIp(int i) {
return ((i >> 24 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF);
}
public static Long ipToInt(String addr) {
String[] addrArray = addr.split("\\.");
long num = 0;
for (int i=0;i<addrArray.length;i++) {
int power = 3-i;
num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
}
return num;
}
我将从将字符串转换为八位字节开始:
static final String DEC_IPV4_PATTERN = "^(([0-1]?\\d{1,2}\\.)|(2[0-4]\\d\\.)|(25[0-5]\\.)){3}(([0-1]?\\d{1,2})|(2[0-4]\\d)|(25[0-5]))$";
static byte[] toOctets(String address){
if(address==null){
throw new NullPointerException("The IPv4 address cannot be null.");
}
if(!address.matches(DEC_IPV4_PATTERN)){
throw new IllegalArgumentException(String.format("The IPv4 address is invalid:%s ",address));
}
//separate octets into individual strings
String[] numbers = address.split("\\.");
//convert octets to bytes.
byte[] octets = new byte[4];
for(int i = 0; i < octets.length; i++){
octets[i] = Integer.valueOf(numbers[i]).byteValue();
}
return octets;
}
然后将八位组转换为 BigInteger,因为它接受一个字节数组,然后从它转换为一个整数:
static int toInteger(byte[] octets){
if(octets==null){
throw new NullPointerException("The array of octets cannot be null");
}
if(octets.length != 4){
throw new IllegalArgumentException(String.format("The byte array must contain 4 octets: %d",octets.length));
}
return new BigInteger(octets).intValue();
}
从这里,您可以简单地执行以下操作:
String address = "127.0.0.1";
System.out.println(toInteger(toOctets(address)));
或者创建一个名为ip2long(String address )
这适用于 IPv4 和 IPv6:
public static String toLongString(final String ip) {
try {
final InetAddress address = InetAddress.getByName(ip);
final byte[] bytes = address.getAddress();
final byte[] uBytes = new byte[bytes.length + 1]; // add one byte at the top to make it unsigned
System.arraycopy(bytes, 0, uBytes, 1, bytes.length);
return new BigInteger(uBytes).toString();
} catch (final UnknownHostException e) {
throw new IllegalArgumentException(e);
}
}
我希望下面的链接对您有所帮助,反之亦然
https://www.mkyong.com/java/java-convert-ip-address-to-decimal-number。
以上链接提供的方法:
public static long ipToLong(String ipAddress) {
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]);
result += ip * Math.pow(256, power);
}
return result;
}
public static 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();
}