Here you can find the source of convertBigIntegerIntoInetAddress(final BigInteger i)
public static InetAddress convertBigIntegerIntoInetAddress(final BigInteger i) throws UnknownHostException
//package com.java2s; /******************************************************************************* * This file is part of OpenNMS(R).//from ww w. jav a 2s .co m * * Copyright (C) 2007-2012 The OpenNMS Group, Inc. * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc. * * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. * * OpenNMS(R) is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * OpenNMS(R) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with OpenNMS(R). If not, see: * http://www.gnu.org/licenses/ * * For more information contact: * OpenNMS(R) Licensing <license@opennms.org> * http://www.opennms.org/ * http://www.opennms.com/ *******************************************************************************/ import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; public class Main { public static InetAddress convertBigIntegerIntoInetAddress(final BigInteger i) throws UnknownHostException { if (i.compareTo(BigInteger.ZERO) < 0) { throw new IllegalArgumentException( "BigInteger is negative, cannot convert into an IP address: " + i.toString()); } else { // Note: This function will return the two's complement byte array so there will always // be a bit of value '0' (indicating positive sign) at the first position of the array // and it will be padded to the byte boundry. For example: // // 255.255.255.255 => 00 FF FF FF FF (5 bytes) // 127.0.0.1 => 0F 00 00 01 (4 bytes) // final byte[] bytes = i.toByteArray(); if (bytes.length == 0) { return InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }); } else if (bytes.length <= 4) { // This case covers an IPv4 address with the most significant bit of zero (the MSB // will be used as the two's complement sign bit) final byte[] addressBytes = new byte[4]; int k = 3; for (int j = bytes.length - 1; j >= 0; j--, k--) { addressBytes[k] = bytes[j]; } return InetAddress.getByAddress(addressBytes); } else if (bytes.length <= 5 && bytes[0] == 0) { // This case covers an IPv4 address (4 bytes + two's complement sign bit of zero) final byte[] addressBytes = new byte[4]; int k = 3; for (int j = bytes.length - 1; j >= 1; j--, k--) { addressBytes[k] = bytes[j]; } return InetAddress.getByAddress(addressBytes); } else if (bytes.length <= 16) { // This case covers an IPv6 address with the most significant bit of zero (the MSB // will be used as the two's complement sign bit) final byte[] addressBytes = new byte[16]; int k = 15; for (int j = bytes.length - 1; j >= 0; j--, k--) { addressBytes[k] = bytes[j]; } return InetAddress.getByAddress(addressBytes); } else if (bytes.length <= 17 && bytes[0] == 0) { // This case covers an IPv6 address (16 bytes + two's complement sign bit of zero) final byte[] addressBytes = new byte[16]; int k = 15; for (int j = bytes.length - 1; j >= 1; j--, k--) { addressBytes[k] = bytes[j]; } return InetAddress.getByAddress(addressBytes); } else { throw new IllegalArgumentException( "BigInteger is too large to convert into an IP address: " + i.toString()); } } } }