Java Utililty Methods BigInteger to

List of utility methods to do BigInteger to

Description

The list of methods to do BigInteger to are organized into topic(s).

Method

byte[]toBase_128(BigInteger val)
This is based on bit shifting (using base128()).
ArrayList<Integer> al = base128(val);
byte[] result = new byte[al.size()];
for (int k = 0; k < result.length; k++)
    result[k] = al.get(k).byteValue();
;
return result;
byte[]toByteArray(BigInteger i)
to Byte Array
byte[] array = i.toByteArray();
if (array[0] == 0) {
    array = Arrays.copyOfRange(array, 1, array.length);
return array;
byte[]toByteArray(final BigInteger b)
Converts a BigInteger to a little endian 32 byte representation.
if (b.compareTo(BigInteger.ONE.shiftLeft(256)) >= 0) {
    throw new RuntimeException("only numbers < 2^256 are allowed");
final byte[] bytes = new byte[32];
final byte[] original = b.toByteArray();
final int offset = original.length > 32 ? original.length - 32 : 0;
for (int i = 0; i < original.length - offset; i++) {
    bytes[original.length - i - offset - 1] = original[i + offset];
...
byte[]toByteArray(final BigInteger value, final int numBytes)
Converts a BigInteger to a little endian byte array.
final byte[] outputBytes = new byte[numBytes];
final byte[] bigIntegerBytes = value.toByteArray();
int copyStartIndex = (0x00 == bigIntegerBytes[0]) ? 1 : 0;
int numBytesToCopy = bigIntegerBytes.length - copyStartIndex;
if (numBytesToCopy > numBytes) {
    copyStartIndex += numBytesToCopy - numBytes;
    numBytesToCopy = numBytes;
for (int i = 0; i < numBytesToCopy; ++i) {
    outputBytes[i] = bigIntegerBytes[copyStartIndex + numBytesToCopy - i - 1];
return outputBytes;
byte[]toByteArrayUnsigned(BigInteger bi)
to Byte Array Unsigned
byte[] extractedBytes = bi.toByteArray();
int skipped = 0;
boolean skip = true;
for (byte b : extractedBytes) {
    boolean signByte = b == (byte) 0x00;
    if (skip && signByte) {
        skipped++;
        continue;
...
byte[]toBytes(BigInteger bigInt, int expectedSize)
to Bytes
byte[] tempArray = bigInt.toByteArray();
byte[] resArray = new byte[expectedSize];
if (tempArray.length > expectedSize) {
    System.arraycopy(tempArray, (tempArray.length - expectedSize), resArray, 0, expectedSize);
    return resArray;
} else {
    return tempArray;
byte[]toBytesUnsigned(final BigInteger bigInt)
Returns a byte array representation of the specified big integer without the sign bit.
int bitlen = bigInt.bitLength();
bitlen = ((bitlen + 7) >> 3) << 3;
final byte[] bigBytes = bigInt.toByteArray();
if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
    return bigBytes;
int startSrc = 0;
int len = bigBytes.length;
...
voidtoDecimal(BigInteger value, byte[] buffer, int offset, int length, int itemLength, byte pos)
Converts a value to DECIMAL or PACF format, writing it into the buffer at the specified position.
if (value.bitLength() < 63) {
    toDecimal(value.longValue(), buffer, offset, length, itemLength, pos);
    return;
BigInteger[] topAndBottom = value.divideAndRemainder(TEN_TO_THE_SEVENTEENTH);
int byteOfSeventeethDigit = offset + (length / 2) - 8;
long bottom = topAndBottom[1].longValue();
toDecimal(bottom, buffer, byteOfSeventeethDigit, 17, 9, pos);
...
StringtoEvenLengthHex(BigInteger value)
Convert a big integer into hex string.
String result = value.toString(16);
if (result.length() % 2 != 0) {
    result = "0" + result;
return result;
byte[]toFixedLenByteArray(BigInteger x, int resultByteLen)
Fit (stretch or shrink) the given positive BigInteger into a byte[] of resultByteLen bytes without losing precision.
if (x.signum() != 1)
    throw new IllegalArgumentException("BigInteger not positive.");
byte[] x_bytes = x.toByteArray();
int x_len = x_bytes.length;
if (x_len <= 0)
    throw new IllegalArgumentException("BigInteger too small.");
int x_off = (x_bytes[0] == 0) ? 1 : 0;
x_len -= x_off;
...