NumberUtils { public static byte[] positiveToByteArray(final BigInteger positiveNumber) { if (null == positiveNumber) { return new byte[0]; } assertTrue(positiveNumber.compareTo(BigInteger.ZERO) >= 0, "Argument must greater than or equals to 0"); final byte[] raw = positiveNumber.toByteArray(); final int positiveByteCapacity = positiveNumber.equals(BigInteger.ZERO) ? 1 : (positiveNumber.bitLength() + 7) >>> 3; if (raw.length > positiveByteCapacity) { return Arrays.copyOfRange(raw, 1, raw.length); } return raw; }  @SuppressWarnings("unchecked") static T convert(final Number number, final Class<T> targetClass); @SuppressWarnings("unchecked") static T parse(final String text, final Class<?> clazz); static T parse(final String text, final Class<T> targetClass,
      final NumberFormat numberFormat); static byte[] positiveToByteArray(final BigInteger positiveNumber); static BigInteger byteArrayToPositive(final byte[] rawBytes);  }
@Test public void testPositiveToByteArray() { final Object[][] testParameters = new Object[][] { {null, new byte[0]}, {BigInteger.valueOf(0L), new byte[] {(byte) 0x00}}, {BigInteger.valueOf(127L), new byte[] {(byte) 0x7F}}, {BigInteger.valueOf(255L), new byte[] {(byte) 0xFF}} }; for (final Object[] testParameter : testParameters) { final BigInteger value = (BigInteger) testParameter[0]; final byte[] expected = (byte[]) testParameter[1]; final byte[] actual = NumberUtils.positiveToByteArray(value); assertTrue(Arrays.equals(expected, actual)); } }