Here you can find the source of bigIntegerToBytes(BigInteger b, int numBytes)
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes)
//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; }//w ww . j a va 2 s .co m 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; } }