Java ip2long等价物
您好,我有一个数据库可以从中选择IP位置>Java ip2long等价物,java,php,Java,Php,您好,我有一个数据库可以从中选择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
脚本是用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;
}
公共静态长Dot2LongIP(字符串dotteip){
字符串[]addrArray=dotteip.split(“\\”);
long num=0;
对于(inti=0;i我认为Java中没有标准的API来实现这一点,但是
1/InetAddress类提供了获取字节数组的方法
2/如果您确实需要一个整数,可以使用此代码段,可在上找到
公共静态字符串intToIp(inti){
返回((i>>24)&0xFF)+“+
((i>>16)和0xFF)+”+
((i>>8)和0xFF)+”+
(i&0xFF);
}
公共静态长ipToInt(字符串地址){
字符串[]addrArray=addr.split(“\\”);
long num=0;
对于(int i=0;i我将首先将字符串转换为八位字节:
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;
}
从这里,您可以简单地执行以下操作:
String address = "127.0.0.1";
System.out.println(toInteger(toOctets(address)));
或者创建一个名为ip2long(字符串地址)
的函数此函数适用于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);
}
}
我希望下面的链接能帮助你,反之亦然
从以上链接提供的方法:
公共静态长IPTOLOG(字符串ipAddress){
字符串[]ipAddressInArray=ipAddress.split(“\\”);
长结果=0;
对于(int i=0;i>8;
}
返回result.toString();
}
公共静态长ipToLong(字符串stringIp){
返回Arrays.stream(stringIp.split(“\\”))
.mapToLong(Long::parseLong)
.减少(0,
(a,b)->(a>24)和0xFF)+”+
((longIp>>16)和0xFF)+”+
((longIp>>8)和0xFF)+“+
(p&0xFF);
}
这方面的基本公式可以在很多地方找到。Javascript端口可能重复:谢谢,我们必须尝试它,以确保获得准确的结果。您为我节省了时间:)不要尝试“太多”,事实上,看起来我们有相同的源代码,除了我引用了我的;=)
String address = "127.0.0.1";
System.out.println(toInteger(toOctets(address)));
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);
}
}
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();
}
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);
}