Here you can find the source of toByteArray(final BigInteger value, final int numBytes)
Parameter | Description |
---|---|
value | The value to convert. |
numBytes | The number of bytes in the destination array. |
public static byte[] toByteArray(final BigInteger value, final int numBytes)
//package com.java2s; //License from project: Open Source License import java.math.BigInteger; public class Main { /**//from w w w .j a v a 2 s. c om * Converts a BigInteger to a little endian byte array. * * @param value The value to convert. * @param numBytes The number of bytes in the destination array. * @return The resulting little endian byte array. */ public static byte[] toByteArray(final BigInteger value, final int numBytes) { 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; } }