@Test public void testToBytesPadded() { assertThat(Numeric.toBytesPadded(BigInteger.TEN, 1), is(new byte[] { 0xa })); assertThat(Numeric.toBytesPadded(BigInteger.TEN, 8), is(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0xa })); assertThat(Numeric.toBytesPadded(BigInteger.valueOf(Integer.MAX_VALUE), 4), is(new byte[] { 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff })); }
public static byte[] toBytesPadded(BigInteger value, int length) { byte[] result = new byte[length]; byte[] bytes = value.toByteArray(); int bytesLength; int srcOffset; if (bytes[0] == 0) { bytesLength = bytes.length - 1; srcOffset = 1; } else { bytesLength = bytes.length; srcOffset = 0; } if (bytesLength > length) { throw new RuntimeException("Input is too large to put in byte array of size " + length); } int destOffset = length - bytesLength; System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); return result; }
Numeric { public static byte[] toBytesPadded(BigInteger value, int length) { byte[] result = new byte[length]; byte[] bytes = value.toByteArray(); int bytesLength; int srcOffset; if (bytes[0] == 0) { bytesLength = bytes.length - 1; srcOffset = 1; } else { bytesLength = bytes.length; srcOffset = 0; } if (bytesLength > length) { throw new RuntimeException("Input is too large to put in byte array of size " + length); } int destOffset = length - bytesLength; System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); return result; } }
Numeric { public static byte[] toBytesPadded(BigInteger value, int length) { byte[] result = new byte[length]; byte[] bytes = value.toByteArray(); int bytesLength; int srcOffset; if (bytes[0] == 0) { bytesLength = bytes.length - 1; srcOffset = 1; } else { bytesLength = bytes.length; srcOffset = 0; } if (bytesLength > length) { throw new RuntimeException("Input is too large to put in byte array of size " + length); } int destOffset = length - bytesLength; System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); return result; } private  Numeric(); }
Numeric { public static byte[] toBytesPadded(BigInteger value, int length) { byte[] result = new byte[length]; byte[] bytes = value.toByteArray(); int bytesLength; int srcOffset; if (bytes[0] == 0) { bytesLength = bytes.length - 1; srcOffset = 1; } else { bytesLength = bytes.length; srcOffset = 0; } if (bytesLength > length) { throw new RuntimeException("Input is too large to put in byte array of size " + length); } int destOffset = length - bytesLength; System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); return result; } private  Numeric(); static String encodeQuantity(BigInteger value); static BigInteger decodeQuantity(String value); static String cleanHexPrefix(String input); static String prependHexPrefix(String input); static boolean containsHexPrefix(String input); static BigInteger toBigInt(byte[] value, int offset, int length); static BigInteger toBigInt(byte[] value); static BigInteger toBigInt(String hexValue); static BigInteger toBigIntNoPrefix(String hexValue); static String toHexStringWithPrefix(BigInteger value); static String toHexStringNoPrefix(BigInteger value); static String toHexStringNoPrefix(byte[] input); static String toHexStringWithPrefixZeroPadded(BigInteger value, int size); static String toHexStringNoPrefixZeroPadded(BigInteger value, int size); static byte[] toBytesPadded(BigInteger value, int length); static byte[] hexStringToByteArray(String input); static String toHexString(byte[] input, int offset, int length, boolean withPrefix); static String toHexString(byte[] input); static byte asByte(int m, int n); static boolean isIntegerValue(BigDecimal value); }
Numeric { public static byte[] toBytesPadded(BigInteger value, int length) { byte[] result = new byte[length]; byte[] bytes = value.toByteArray(); int bytesLength; int srcOffset; if (bytes[0] == 0) { bytesLength = bytes.length - 1; srcOffset = 1; } else { bytesLength = bytes.length; srcOffset = 0; } if (bytesLength > length) { throw new RuntimeException("Input is too large to put in byte array of size " + length); } int destOffset = length - bytesLength; System.arraycopy(bytes, srcOffset, result, destOffset, bytesLength); return result; } private  Numeric(); static String encodeQuantity(BigInteger value); static BigInteger decodeQuantity(String value); static String cleanHexPrefix(String input); static String prependHexPrefix(String input); static boolean containsHexPrefix(String input); static BigInteger toBigInt(byte[] value, int offset, int length); static BigInteger toBigInt(byte[] value); static BigInteger toBigInt(String hexValue); static BigInteger toBigIntNoPrefix(String hexValue); static String toHexStringWithPrefix(BigInteger value); static String toHexStringNoPrefix(BigInteger value); static String toHexStringNoPrefix(byte[] input); static String toHexStringWithPrefixZeroPadded(BigInteger value, int size); static String toHexStringNoPrefixZeroPadded(BigInteger value, int size); static byte[] toBytesPadded(BigInteger value, int length); static byte[] hexStringToByteArray(String input); static String toHexString(byte[] input, int offset, int length, boolean withPrefix); static String toHexString(byte[] input); static byte asByte(int m, int n); static boolean isIntegerValue(BigDecimal value);  }