Java examples for java.math:BigInteger
Return the value of BigInteger as a byte array.
//package com.java2s; import java.math.BigInteger; public class Main { public static void main(String[] argv) throws Exception { BigInteger value = new BigInteger("1234"); System.out.println(java.util.Arrays .toString(toMinimalByteArray(value))); }/* w w w . j av a2 s .c o m*/ /** * Return the value of <tt>big</tt> as a byte array. Although BigInteger has * such a method, it uses an extra bit to indicate the sign of the number. * For elliptic curve cryptography, the numbers usually are positive. Thus, * this helper method returns a byte array of minimal length, ignoring the * sign of the number. * * @param value * the <tt>BigInteger</tt> value to be converted to a byte array * @return the value <tt>big</tt> as byte array */ public static byte[] toMinimalByteArray(BigInteger value) { byte[] valBytes = value.toByteArray(); if ((valBytes.length == 1) || (value.bitLength() & 0x07) != 0) { return valBytes; } byte[] result = new byte[value.bitLength() >> 3]; System.arraycopy(valBytes, 1, result, 0, result.length); return result; } }