スキップしてメイン コンテンツに移動

投稿

ラベル(IPv4)が付いた投稿を表示しています

Java: Dotted IPv4 Address To BigInteger

IPv4 Address to Long I have written code for converting dotted IPv4 IP address to BigInteger value in Java. This code is inspired by PHP's ip2long function. The function is useful when you compare IP addresses based on numeric magnitude relationship. Java Code public static BigInteger dotIPv4_to_BigInteger(String dottedIP) { String[] addrArray = dottedIP.split("\\."); BigInteger num = BigInteger.ZERO; BigInteger block = BigInteger.valueOf(256); for (int i = 0; i < addrArray.length; i++) { int power = 3-i; BigInteger value = BigInteger.valueOf(Integer.parseInt(addrArray[i]) % 256); value = value.multiply(block.pow(power)); num = num.add(value); } return num; } Here is an example. // following code output "2071690107" System.out.println(dotIPv4_to_BigInteger("123.123.123.123"));