简体   繁体   English

在Java中将IP地址转换为其十进制等价物?

[英]Convert an ip address to its decimal equivalent in Java?

I would like to write a method but have no idea where to start concerning converting a String to decimal format.我想编写一个方法,但不知道从哪里开始将字符串转换为十进制格式。

Here's an example of what I would like:这是我想要的一个例子:

I have the following string:我有以下字符串:

String ipAddress = "192.168.1.10 "

And I want to convert it to its decimal equivalent:我想将其转换为十进制等效值:

3232235786

Many thanks!非常感谢!

How about:怎么样:

new BigInteger( InetAddress.getByName("1.1.1.1").getAddress() ).intValue()

If I remember correctly this should do the trick... obviously use your IP rather than 1.1.1.1.如果我没记错的话,这应该可以解决问题……显然使用您的 IP 而不是 1.1.1.1。

How about (tested & working):怎么样(测试和工作):

    String ipAddress = "192.168.1.10";
    String[] addrArray = addr.split("\\.");

    long ipDecimal = 0;

    for (int i = 0; i < addrArray.length; i++) {

        int power = 3 - i;
        ipDecimal += ((Integer.parseInt(addrArray[i]) % 256 * Math.pow(256, power)));
    }

    System.out.println(ipDecimal);
public class Main
{
    public static void main(String[] args)
    {
        String ip="192.168.1.10";
        String[] addrArray = ip.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)));
        }
        System.out.println(num);
    }
}

Output:输出:

3232235786

换算方程:

A.B.C.D = D + (C * 256) + (B * 256 * 256) + (A * 256 * 256 * 256)

convert it to binary then convert that to decimal将其转换为二进制然后将其转换为十进制

or try this或者试试这个

Convert each segment to an 8-bit binary number, concatenate all 4 8-bit binary numbers into one 32-bit binary number, then convert than 32-bit binary number to decimal and you have your answer.将每个段转换为一个 8 位二进制数,将所有 4 个 8 位二进制数连接成一个 32 位二进制数,然后将 32 位二进制数转换为十进制数,您就有了答案。

EDIT: Here's how you can convert numbers to and from binary:编辑:以下是将数字转换为二进制或从二进制转换的方法:

public static int binaryToDecimal(String bin) {
    int result = 0;
    int len = bin.length();
    for(int i = 0; i < len; i++) {
        result += Integer.parseInt(bin.charAt(i) +  "") * Math.pow(2, len - i - 1);
    }
    return result;
}

public static String decimalToBinary(int num) {
    String result = "";
    while(true) {
        result += num % 2;
        if(num < 2)
            break;
        num = num / 2;
    }
    result = reverse(result);
    return result;
}

public static String reverse(String str) {
    String result = "";
    for(int i = str.length() - 1; i >= 0; i--)
        result += str.charAt(i);
    return result;
}

I used the following code for translating an IP to an Integer in order to use IP2Country-Databases by MaxMind or IP2Location .我用下面的代码,以便通过使用IP2Country-数据库转换的IP为一个整数的MaxMindIP2Location

You can copy paste this code into a helper class for your own needs.您可以根据自己的需要将此代码复制粘贴到帮助程序类中。

public static int ip2Int(String ip) {
    int ipInt = 0;
    String[] tokens = ip.split("\\.");
    int factor = 256*256*256;
    for (String t:tokens) {
        ipInt += new Integer(t).intValue()*factor;
        factor = factor/256;
    }
    return ipInt;
}

You can convert string represented IP address (doesn't matter if IPv4 or IPv6) to decimal by converting it to BigInteger:您可以通过将字符串表示的 IP 地址(无论是 IPv4 还是 IPv6)转换为 BigInteger 来将其转换为十进制:

private static BigInteger ipToDecimal(String ip) {
    return new BigInteger(1, InetAddresses.forString(ip).getAddress());
}

And when necessary then you can convert it back by:如有必要,您可以通过以下方式将其转换回来:

private static String decimalToIp(String decimal) throws Exception {
    BigInteger parsed = new BigInteger(decimal);

    if (parsed.compareTo(BigInteger.valueOf(4294967295L)) > 0) {
        return Joiner.on(":").join(Splitter.fixedLength(4).split(parsed.toString(16)));
    } else {
        return InetAddress.getByName(decimal).getHostAddress();
    }
}

Converting back is not perfect - you must count with IPv4 mapped addresses see wiki转换回来并不完美 - 您必须计算 IPv4 映射地址, 请参阅 wiki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM