Java BigInteger Calculate getIpAddressByBigInteger(BigInteger[] bigs)

Here you can find the source of getIpAddressByBigInteger(BigInteger[] bigs)

Description

get Ip Address By Big Integer

License

Apache License

Declaration

public static String getIpAddressByBigInteger(BigInteger[] bigs) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String getIpAddressByBigInteger(BigInteger[] bigs) {
        if (bigs.length != 4 && bigs.length != 16) {
            throw new IllegalArgumentException();
        }/* w  w  w .ja va  2 s  . c  o  m*/
        byte[] bytes = new byte[bigs.length];
        for (int i = 0; i < bigs.length; i++) {
            bytes[i] = (byte) (bigs[i].byteValue() & 0xff);
        }
        return getIpAddressByHexaBytes(bytes);
    }

    public static String getIpAddressByHexaBytes(byte[] bytes) {
        StringBuffer sb = null;
        if (bytes.length == 4) {
            for (byte b : bytes) {
                if (sb == null) {
                    sb = new StringBuffer().append((int) b);
                } else {
                    sb.append(".").append((int) b);
                }
            }
        } else if (bytes.length == 16) {
            List<String> hexa = parseBytesAsHexString(bytes);
            for (String h : hexa) {
                if (sb == null) {
                    sb = new StringBuffer().append(h);
                } else {
                    sb.append(":").append(h);
                }
            }
        }
        return (sb == null ? "" : sb.toString());
    }

    private static List<String> parseBytesAsHexString(byte[] bytes) {
        List<String> result = new ArrayList<String>();
        for (byte b : bytes) {
            result.add(Integer.toHexString(b & 0xff));
        }
        return result;
    }
}

Related

  1. getDigitCount(BigInteger number)
  2. getHexString(BigInteger bigInt)
  3. getHistogramBigInt(List data, int breaks)
  4. getIntegral(String number, BigInteger def)
  5. getInterfaceName(final BigInteger datapathid, final String portName, final Integer vlanId)
  6. getIpv4AddrFromNumber(BigInteger addr)
  7. getIpv6AddrFromNumber(BigInteger addr)
  8. getJsonFromBigIntArray(JsonGenerator jg, BigInteger[] array, String pf)
  9. getKeyFromBigInteger(final BigInteger value, final int numBytes)