2

メソッドを書きたいのですが、文字列を10進形式に変換することに関してどこから始めればよいのかわかりません。

これが私が欲しいものの例です:

私は次の文字列を持っています:

String ipAddress = "192.168.1.10 "

そして、それを同等の10進数に変換したいと思います。

3232235786

どうもありがとう!

4

8 に答える 8

4

どうですか:

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

私が正しく覚えていれば、これでうまくいくはずです...明らかに1.1.1.1ではなくIPを使用してください。

于 2012-07-18T18:59:45.817 に答える
2

どうですか(テスト済みで動作中):

    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);
于 2012-07-18T18:51:16.307 に答える
1
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);
    }
}

出力:

3232235786
于 2012-07-18T18:56:39.680 に答える
1

変換の方程式:

A.B.C.D = D + (C * 256) + (B * 256 * 256) + (A * 256 * 256 * 256)
于 2014-06-01T14:05:20.907 に答える
0

2進数に変換してから、10進数に変換します

またはこれを試してください

于 2012-07-18T18:49:51.773 に答える
0

各セグメントを8ビットの2進数に変換し、4つの8ビットの2進数すべてを1つの32ビットの2進数に連結してから、32ビットの2進数を10進数に変換すると、答えが得られます。

編集:数値を2進数に変換する方法と2進数に変換する方法は次のとおりです。

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;
}
于 2012-07-18T18:51:54.893 に答える
0

IP2Countryを使用するためにIPを整数に変換するために次のコードを使用しました-MaxMindまたはIP2Locationによるデータベース。

このコードをコピーして、必要に応じてヘルパークラスに貼り付けることができます。

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;
}
于 2014-01-10T10:00:43.093 に答える
0

文字列で表されるIPアドレス(IPv4またはIPv6のどちらでもかまいません)をBigIntegerに変換することにより、10進数に変換できます。

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

そして、必要に応じて、次の方法で元に戻すことができます。

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

逆変換は完全ではありません-IPv4マップアドレスでカウントする必要がありますwikiを参照してください

于 2020-09-30T11:23:08.353 に答える