List of utility methods to do BigInteger Calculate
int | sumOfDigits(BigInteger n) sum Of Digits int sum = 0; while (!n.equals(BigInteger.ZERO)) { sum += n.remainder(BigInteger.TEN).intValue(); n = n.divide(BigInteger.TEN); return sum; |
byte[] | trim(BigInteger bi) Returns the byte array representation of the given big integer with the leading zero byte (if any) trimmed off. byte[] buf = bi.toByteArray(); if (buf[0] == 0x00 && !bi.equals(BigInteger.ZERO)) { return trim(buf, 1, buf.length - 1); } else { return buf; |
byte[] | trim(BigInteger n) Treats the input as the MSB representation of a number, and lop off leading zero elements. byte[] in = n.toByteArray(); if (in.length == 0 || in[0] != 0) return in; int len = in.length; int i = 1; while (in[i] == 0 && i < len) ++i; byte[] ret = new byte[len - i]; ... |
BigInteger | trimToPrecision(BigInteger number, int precision) trim To Precision String n = number.toString(); if (n.length() > precision) { n = n.substring(n.length() - precision); return new BigInteger(n); |
void | uint64ToByteStreamLE(BigInteger val, OutputStream stream) uint To Byte Stream LE byte[] bytes = val.toByteArray(); if (bytes.length > 8) { throw new RuntimeException("Input too large to encode into a uint64"); bytes = reverseBytes(bytes); stream.write(bytes); if (bytes.length < 8) { for (int i = 0; i < 8 - bytes.length; i++) ... |
byte[] | unsignedBigIntergerToByteArray(BigInteger bigInteger) Convert a BigInteger to a byte-array, but treat the byte-array given from the BigInteger as unsigned and removing any leading zero bytes; e.g. if (bigInteger == null) { return null; byte[] integerBytes = bigInteger.toByteArray(); byte[] unsignedIntegerBytes; if ((integerBytes.length > 0) && (integerBytes[0] == 0x00)) { unsignedIntegerBytes = new byte[integerBytes.length - 1]; for (int i = 0; i < unsignedIntegerBytes.length; i++) { ... |
BigInteger | unsignedLongToBigInteger(byte[] source) unsigned Long To Big Integer byte[] result = new byte[source.length + 1]; result[0] = 0; for (int i = 1; i < result.length; i++) result[i] = source[i - 1]; return new BigInteger(result); |
BigInteger | unsignedToBigInteger(long l) unsigned To Big Integer final BigInteger bi = BigInteger.valueOf(l); return l >= 0 ? bi : bi.add(BI_2_64); |
void | writeBigInteger(BigInteger integer, DataOutputStream out) Write a (reasonably short) BigInteger to a stream. if (integer.signum() == -1) { throw new IllegalStateException("Negative BigInteger!"); byte[] buf = integer.toByteArray(); if (buf.length > Short.MAX_VALUE) { throw new IllegalStateException("Too long: " + buf.length); out.writeShort((short) buf.length); ... |
void | writeBigInteger(BigInteger m, int n, OutputStream os) write Big Integer byte[] temp = new byte[n]; BigInteger mask = BigInteger.valueOf(0xFF); for (int j = 0; j < n; j++) { temp[j] = (byte) m.and(mask).intValue(); m = m.shiftRight(8); os.write(temp); |