Here you can find the source of getHexString(BigInteger bigInt)
Parameter | Description |
---|---|
bigInt | Big integer |
public static String getHexString(BigInteger bigInt)
//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)); } }