Java ip2long等价

时间:2012-05-29 13:58:50

标签: java php

您好我有一个数据库可以从>

中选择IP位置

脚本在php中,我将它转换为java,但我不知道java中的ip2long('127.0.0.1' ));是等价的

6 个答案:

答案 0 :(得分:2)

基本上,这会将您的点分IP地址字符串转换为long。

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;    
}

答案 1 :(得分:2)

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);
}

答案 2 :(得分:1)

我认为在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;

}

答案 3 :(得分:1)

我首先将字符串转换为八位字节:

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 )

的函数

答案 4 :(得分:0)

这适用于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);
    }
}

答案 5 :(得分:0)

我希望下面的链接能为您提供帮助,反之亦然

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();
}