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; 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; } }