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

StringbigIntegerToAddress(BigInteger ethereumAddress)
Get the address as short string.
String address = ethereumAddress.toString(16);
while (address.length() < 40) {
    address = "0" + address;
if (!isValidAddress(address)) {
    throw new Error("not an address");
return "0x" + address;
...
StringbigIntegerToBase64(BigInteger value)
Converts a BigInteger, e.g.
byte[] data = value.toByteArray();
String result = "";
if ((data[0] & 0x80) != 0) {
    result = "0";
for (int i = 0; i < data.length; i++) {
    result += (char) data[i];
return stringToBase64(result);
int[]bigIntegerToBitArray(BigInteger x, int length)
big Integer To Bit Array
int[] result = new int[length];
for (int i = 0; i < result.length; i++) {
    result[i] = x.testBit(i) ? 1 : 0;
return result;
byte[]BigIntegerToByteArrayWithoutSign(BigInteger value)
Big Integer To Byte Array Without Sign
byte[] bytes = value.toByteArray();
if (bytes[0] == (byte) 0) {
    bytes = copyArray(bytes, 1, bytes.length - 1);
return bytes;
byte[]bigIntegerToBytes(BigInteger b, int numBytes)
big Integer To Bytes
if (b == null)
    return null;
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
return bytes;
...
byte[]bigIntegerToBytes(BigInteger b, int numBytes)
big Integer To Bytes
if (b == null) {
    return null;
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
...
byte[]bigIntegerToBytes(BigInteger b, int numBytes)
The regular java.math.BigInteger#toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
return bytes;
byte[]bigIntegerToBytes(BigInteger b, int numBytes)
The regular java.math.BigInteger#toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
if (b == null) {
    return EMPTY_BYTE_ARRAY;
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
...
byte[]bigIntegerToBytes(BigInteger integer)
This function converts a big integer to its corresponding byte array format.
return integer.toByteArray();
voidbigIntegerToBytes(BigInteger n, byte[] data, int[] offset)
Write the bytes representing n into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
byte[] bytes = n.toByteArray();
intToBytes(bytes.length, data, offset);
offset[0] += memcpy(data, offset[0], bytes, 0, bytes.length);