@Test public void testBigIntToBytesPadded() { byte[] smallNumBytes = new byte[]{0, 0, 0, 0, 0, 1, 1, 1}; String smallNum = "65793"; byte[] mediumNumBytes = new byte[]{0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; String mediumNum = "310698676526526814092329217"; assertArrayEquals(smallNumBytes, NumericUtil.bigIntegerToBytesWithZeroPadded(new BigInteger(smallNum), 8)); assertArrayEquals(mediumNumBytes, NumericUtil.bigIntegerToBytesWithZeroPadded(new BigInteger(mediumNum), 16)); }
public static byte[] bigIntegerToBytesWithZeroPadded(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; }
NumericUtil { public static byte[] bigIntegerToBytesWithZeroPadded(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; } }
NumericUtil { public static byte[] bigIntegerToBytesWithZeroPadded(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; }  }
NumericUtil { public static byte[] bigIntegerToBytesWithZeroPadded(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; }  static byte[] generateRandomBytes(int size); static boolean isValidHex(String value); static String cleanHexPrefix(String input); static String prependHexPrefix(String input); static BigInteger bytesToBigInteger(byte[] value, int offset, int length); static BigInteger bytesToBigInteger(byte[] value); static BigInteger hexToBigInteger(String hexValue); static String bigIntegerToHex(BigInteger value); static String bigIntegerToHexWithZeroPadded(BigInteger value, int size); static byte[] bigIntegerToBytesWithZeroPadded(BigInteger value, int length); static byte[] hexToBytes(String input); static byte[] hexToBytesLittleEndian(String input); static byte[] reverseBytes(byte[] bytes); static String bytesToHex(byte[] input); static String beBigEndianHex(String hex); static String beLittleEndianHex(String hex); static int bytesToInt(byte[] bytes); static byte[] intToBytes(int intValue); }
NumericUtil { public static byte[] bigIntegerToBytesWithZeroPadded(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; }  static byte[] generateRandomBytes(int size); static boolean isValidHex(String value); static String cleanHexPrefix(String input); static String prependHexPrefix(String input); static BigInteger bytesToBigInteger(byte[] value, int offset, int length); static BigInteger bytesToBigInteger(byte[] value); static BigInteger hexToBigInteger(String hexValue); static String bigIntegerToHex(BigInteger value); static String bigIntegerToHexWithZeroPadded(BigInteger value, int size); static byte[] bigIntegerToBytesWithZeroPadded(BigInteger value, int length); static byte[] hexToBytes(String input); static byte[] hexToBytesLittleEndian(String input); static byte[] reverseBytes(byte[] bytes); static String bytesToHex(byte[] input); static String beBigEndianHex(String hex); static String beLittleEndianHex(String hex); static int bytesToInt(byte[] bytes); static byte[] intToBytes(int intValue);  }