Java BigInteger Calculate getHexString(BigInteger bigInt)

Here you can find the source of getHexString(BigInteger bigInt)

Description

Get hex string for the supplied big integer: "0x" where hex string is outputted in groups of exactly four characters sub-divided by spaces.

License

Apache License

Parameter

Parameter Description
bigInt Big integer

Return

Hex string

Declaration

public static String getHexString(BigInteger bigInt) 

Method Source Code

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

import java.math.BigInteger;

public class Main {
    /****************************************************************************
     * Get hex string for the supplied big integer: "0x<hex string>" where hex
     * string is outputted in groups of exactly four characters sub-divided by
     * spaces.// www  .j  av  a  2s  .c  om
     * 
     * @param bigInt
     *            Big integer
     * @return Hex string
     */
    public static String getHexString(BigInteger bigInt) {
        // Convert number to hex string
        String hex = bigInt.toString(16).toUpperCase();

        // Get number padding bytes
        int padding = (4 - (hex.length() % 4));

        // Insert any required padding to get groups of exactly 4 characters
        if ((padding > 0) && (padding < 4)) {
            StringBuilder sb = new StringBuilder(hex);

            for (int i = 0; i < padding; i++) {
                sb.insert(0, '0');
            }

            hex = sb.toString();
        }

        // Output with leading "0x" and spaces to form groups
        StringBuilder strBuff = new StringBuilder();

        strBuff.append("0x");

        for (int i = 0; i < hex.length(); i++) {
            strBuff.append(hex.charAt(i));

            if ((((i + 1) % 4) == 0) && ((i + 1) != hex.length())) {
                strBuff.append(' ');
            }
        }

        return strBuff.toString();
    }

    /**
     * Get hex string for the supplied byte array: "0x<hex string>" where hex
     * string is outputted in groups of exactly four characters sub-divided by
     * spaces.
     * 
     * @param bytes
     *            Byte array
     * @return Hex string
     */
    public static String getHexString(byte[] bytes) {
        return getHexString(new BigInteger(1, bytes));
    }
}

Related

  1. getClearExpPipe(BigInteger M)
  2. getCreateLocalNextHopJobKey(Long vpnId, BigInteger dpnId, String prefix)
  3. getDateOf(BigInteger fileTime)
  4. getDateOf(final BigInteger fileTime)
  5. getDigitCount(BigInteger number)
  6. getHistogramBigInt(List data, int breaks)
  7. getIntegral(String number, BigInteger def)
  8. getInterfaceName(final BigInteger datapathid, final String portName, final Integer vlanId)
  9. getIpAddressByBigInteger(BigInteger[] bigs)