@Test public void success_ToByteArray_from_BigInteger_2() { byte[] input1 = new byte[]{5, -4, -22, 3, 94, -115, -22, 27, 12, 35, -14, 1, -26, 3, -44, 13, -113, -31, 93, 29, 2, -33, -72, 42, 14, -9, 13, 40, -28, 79, -13, 38}; byte[] expectedOutput1 = new byte[]{5, -4, -22, 3, 94, -115, -22, 27, 12, 35, -14, 1, -26, 3, -44, 13, -113, -31, 93, 29, 2, -33, -72, 42, 14, -9, 13, 40, -28, 79, -13}; byte[] input2 = new byte[]{-34, 4, -22, 3, 94, -115, -22, 27, 12, 35, -14, 1, -26, 3, -44, 13, -113, -31, 93, 29, 2, -33, -72, 42, 14, -9, 13, 40, -28, 79, -13, 38}; byte[] expectedOutput2 = new byte[]{-34, 4, -22, 3, 94, -115, -22, 27, 12, 35, -14, 1, -26, 3, -44, 13, -113, -31, 93, 29, 2, -33, -72, 42, 14, -9, 13, 40, -28, 79, -13}; BigInteger integer1 = new BigInteger(1, input1); BigInteger integer2 = new BigInteger(1, input2); assertThat(integer1.toByteArray(), is(input1)); assertThat(integer2.toByteArray(), not(input2)); assertThat(ArrayUtils.toByteArray(integer1, 31), is(expectedOutput1)); assertThat(ArrayUtils.toByteArray(integer2, 31), is(expectedOutput2)); }
public static byte[] toByteArray(BigInteger value, int expectedBytesLength) { byte[] input = value.toByteArray(); int byteTmpLength = input.length; if (byteTmpLength <= expectedBytesLength) { return input; } int copyStartIndex = 0; byte[] result; if (input[0] == 0x00) { copyStartIndex = 1; } result = new byte[expectedBytesLength]; System.arraycopy(input, copyStartIndex, result, 0, expectedBytesLength); return result; }
ArrayUtils { public static byte[] toByteArray(BigInteger value, int expectedBytesLength) { byte[] input = value.toByteArray(); int byteTmpLength = input.length; if (byteTmpLength <= expectedBytesLength) { return input; } int copyStartIndex = 0; byte[] result; if (input[0] == 0x00) { copyStartIndex = 1; } result = new byte[expectedBytesLength]; System.arraycopy(input, copyStartIndex, result, 0, expectedBytesLength); return result; } }
ArrayUtils { public static byte[] toByteArray(BigInteger value, int expectedBytesLength) { byte[] input = value.toByteArray(); int byteTmpLength = input.length; if (byteTmpLength <= expectedBytesLength) { return input; } int copyStartIndex = 0; byte[] result; if (input[0] == 0x00) { copyStartIndex = 1; } result = new byte[expectedBytesLength]; System.arraycopy(input, copyStartIndex, result, 0, expectedBytesLength); return result; }  }
ArrayUtils { public static byte[] toByteArray(BigInteger value, int expectedBytesLength) { byte[] input = value.toByteArray(); int byteTmpLength = input.length; if (byteTmpLength <= expectedBytesLength) { return input; } int copyStartIndex = 0; byte[] result; if (input[0] == 0x00) { copyStartIndex = 1; } result = new byte[expectedBytesLength]; System.arraycopy(input, copyStartIndex, result, 0, expectedBytesLength); return result; }  static byte[][] split(byte[] bytes, int splitIndex); static int[] reverse(int[] input); static byte[] toByteArray(BigInteger value, int expectedBytesLength); static int[] toBinaryArray(BigInteger integer); static int[] toMutualOppositeForm(BigInteger integer); }
ArrayUtils { public static byte[] toByteArray(BigInteger value, int expectedBytesLength) { byte[] input = value.toByteArray(); int byteTmpLength = input.length; if (byteTmpLength <= expectedBytesLength) { return input; } int copyStartIndex = 0; byte[] result; if (input[0] == 0x00) { copyStartIndex = 1; } result = new byte[expectedBytesLength]; System.arraycopy(input, copyStartIndex, result, 0, expectedBytesLength); return result; }  static byte[][] split(byte[] bytes, int splitIndex); static int[] reverse(int[] input); static byte[] toByteArray(BigInteger value, int expectedBytesLength); static int[] toBinaryArray(BigInteger integer); static int[] toMutualOppositeForm(BigInteger integer);  }