@Test public void bigIntegerToBytes_convertZero() { BigInteger b = BigInteger.valueOf(0); byte[] expected = new byte[]{0b0000_0000}; byte[] actual = Utils.bigIntegerToBytes(b, 1); assertTrue(Arrays.equals(expected, actual)); }
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) { checkArgument(b.signum() >= 0, "b must be positive or zero"); checkArgument(numBytes > 0, "numBytes must be positive"); byte[] src = b.toByteArray(); byte[] dest = new byte[numBytes]; boolean isFirstByteOnlyForSign = src[0] == 0; int length = isFirstByteOnlyForSign ? src.length - 1 : src.length; checkArgument(length <= numBytes, "The given number does not fit in " + numBytes); int srcPos = isFirstByteOnlyForSign ? 1 : 0; int destPos = numBytes - length; System.arraycopy(src, srcPos, dest, destPos, length); return dest; }
Utils { public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) { checkArgument(b.signum() >= 0, "b must be positive or zero"); checkArgument(numBytes > 0, "numBytes must be positive"); byte[] src = b.toByteArray(); byte[] dest = new byte[numBytes]; boolean isFirstByteOnlyForSign = src[0] == 0; int length = isFirstByteOnlyForSign ? src.length - 1 : src.length; checkArgument(length <= numBytes, "The given number does not fit in " + numBytes); int srcPos = isFirstByteOnlyForSign ? 1 : 0; int destPos = numBytes - length; System.arraycopy(src, srcPos, dest, destPos, length); return dest; } }
Utils { public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) { checkArgument(b.signum() >= 0, "b must be positive or zero"); checkArgument(numBytes > 0, "numBytes must be positive"); byte[] src = b.toByteArray(); byte[] dest = new byte[numBytes]; boolean isFirstByteOnlyForSign = src[0] == 0; int length = isFirstByteOnlyForSign ? src.length - 1 : src.length; checkArgument(length <= numBytes, "The given number does not fit in " + numBytes); int srcPos = isFirstByteOnlyForSign ? 1 : 0; int destPos = numBytes - length; System.arraycopy(src, srcPos, dest, destPos, length); return dest; }  }
Utils { public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) { checkArgument(b.signum() >= 0, "b must be positive or zero"); checkArgument(numBytes > 0, "numBytes must be positive"); byte[] src = b.toByteArray(); byte[] dest = new byte[numBytes]; boolean isFirstByteOnlyForSign = src[0] == 0; int length = isFirstByteOnlyForSign ? src.length - 1 : src.length; checkArgument(length <= numBytes, "The given number does not fit in " + numBytes); int srcPos = isFirstByteOnlyForSign ? 1 : 0; int destPos = numBytes - length; System.arraycopy(src, srcPos, dest, destPos, length); return dest; }  static byte[] bigIntegerToBytes(BigInteger b, int numBytes); static void uint16ToByteArrayLE(int val, byte[] out, int offset); static void uint32ToByteArrayLE(long val, byte[] out, int offset); static void uint32ToByteArrayBE(long val, byte[] out, int offset); static void int64ToByteArrayLE(long val, byte[] out, int offset); static void uint16ToByteStreamLE(int val, OutputStream stream); static void uint16ToByteStreamBE(int val, OutputStream stream); static void uint32ToByteStreamLE(long val, OutputStream stream); static void uint32ToByteStreamBE(long val, OutputStream stream); static void int64ToByteStreamLE(long val, OutputStream stream); static void uint64ToByteStreamLE(BigInteger val, OutputStream stream); static int readUint16(byte[] bytes, int offset); static long readUint32(byte[] bytes, int offset); static long readInt64(byte[] bytes, int offset); static long readUint32BE(byte[] bytes, int offset); static int readUint16BE(byte[] bytes, int offset); static int readUint16FromStream(InputStream is); static long readUint32FromStream(InputStream is); static byte[] reverseBytes(byte[] bytes); static byte[] sha256hash160(byte[] input); static BigInteger decodeMPI(byte[] mpi, boolean hasLength); static byte[] encodeMPI(BigInteger value, boolean includeLength); static BigInteger decodeCompactBits(long compact); static long encodeCompactBits(BigInteger value); static Date rollMockClock(int seconds); static Date rollMockClockMillis(long millis); static void setMockClock(); static void setMockClock(long mockClockSeconds); static Date now(); static long currentTimeMillis(); static long currentTimeSeconds(); static String dateTimeFormat(Date dateTime); static String dateTimeFormat(long dateTime); static boolean checkBitLE(byte[] data, int index); static void setBitLE(byte[] data, int index); static void sleep(long millis); static void setMockSleep(boolean isEnable); static void passMockSleep(); static void finishMockSleep(); static int maxOfMostFreq(int... items); static int maxOfMostFreq(List<Integer> items); static boolean isAndroidRuntime(); static boolean isLinux(); static boolean isWindows(); static boolean isMac(); }
Utils { public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) { checkArgument(b.signum() >= 0, "b must be positive or zero"); checkArgument(numBytes > 0, "numBytes must be positive"); byte[] src = b.toByteArray(); byte[] dest = new byte[numBytes]; boolean isFirstByteOnlyForSign = src[0] == 0; int length = isFirstByteOnlyForSign ? src.length - 1 : src.length; checkArgument(length <= numBytes, "The given number does not fit in " + numBytes); int srcPos = isFirstByteOnlyForSign ? 1 : 0; int destPos = numBytes - length; System.arraycopy(src, srcPos, dest, destPos, length); return dest; }  static byte[] bigIntegerToBytes(BigInteger b, int numBytes); static void uint16ToByteArrayLE(int val, byte[] out, int offset); static void uint32ToByteArrayLE(long val, byte[] out, int offset); static void uint32ToByteArrayBE(long val, byte[] out, int offset); static void int64ToByteArrayLE(long val, byte[] out, int offset); static void uint16ToByteStreamLE(int val, OutputStream stream); static void uint16ToByteStreamBE(int val, OutputStream stream); static void uint32ToByteStreamLE(long val, OutputStream stream); static void uint32ToByteStreamBE(long val, OutputStream stream); static void int64ToByteStreamLE(long val, OutputStream stream); static void uint64ToByteStreamLE(BigInteger val, OutputStream stream); static int readUint16(byte[] bytes, int offset); static long readUint32(byte[] bytes, int offset); static long readInt64(byte[] bytes, int offset); static long readUint32BE(byte[] bytes, int offset); static int readUint16BE(byte[] bytes, int offset); static int readUint16FromStream(InputStream is); static long readUint32FromStream(InputStream is); static byte[] reverseBytes(byte[] bytes); static byte[] sha256hash160(byte[] input); static BigInteger decodeMPI(byte[] mpi, boolean hasLength); static byte[] encodeMPI(BigInteger value, boolean includeLength); static BigInteger decodeCompactBits(long compact); static long encodeCompactBits(BigInteger value); static Date rollMockClock(int seconds); static Date rollMockClockMillis(long millis); static void setMockClock(); static void setMockClock(long mockClockSeconds); static Date now(); static long currentTimeMillis(); static long currentTimeSeconds(); static String dateTimeFormat(Date dateTime); static String dateTimeFormat(long dateTime); static boolean checkBitLE(byte[] data, int index); static void setBitLE(byte[] data, int index); static void sleep(long millis); static void setMockSleep(boolean isEnable); static void passMockSleep(); static void finishMockSleep(); static int maxOfMostFreq(int... items); static int maxOfMostFreq(List<Integer> items); static boolean isAndroidRuntime(); static boolean isLinux(); static boolean isWindows(); static boolean isMac(); static final Joiner SPACE_JOINER; static final Splitter WHITESPACE_SPLITTER; static final BaseEncoding HEX; static final int MAX_INITIAL_ARRAY_LENGTH; static volatile Date mockTime; }