Java BigInteger to bigIntegerToBytes(BigInteger b, int numBytes)

Here you can find the source of bigIntegerToBytes(BigInteger b, int numBytes)

Description

big Integer To Bytes

License

Apache License

Declaration

public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) 

Method Source Code

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

import java.math.BigInteger;

public class Main {
    public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
        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;
    }/*from  w  ww . j a v  a2  s . c o  m*/

    public static byte[] bigIntegerToBytes(BigInteger value) {
        if (value == null)
            return null;

        byte[] data = value.toByteArray();

        if (data.length != 1 && data[0] == 0) {
            byte[] tmp = new byte[data.length - 1];
            System.arraycopy(data, 1, tmp, 0, tmp.length);
            data = tmp;
        }
        return data;
    }
}

Related

  1. bigIntegerToAddress(BigInteger ethereumAddress)
  2. bigIntegerToBase64(BigInteger value)
  3. bigIntegerToBitArray(BigInteger x, int length)
  4. BigIntegerToByteArrayWithoutSign(BigInteger value)
  5. bigIntegerToBytes(BigInteger b, int numBytes)
  6. bigIntegerToBytes(BigInteger b, int numBytes)
  7. bigIntegerToBytes(BigInteger b, int numBytes)
  8. bigIntegerToBytes(BigInteger integer)