Open shuijian-xu opened 4 years ago
protected byte[] getBytes(BigInteger big) { byte[] bigBytes = big.toByteArray(); if ((big.bitLength() % 8) != 0) { return bigBytes; } else { byte[] smallerBytes = new byte[big.bitLength() / 8]; System.arraycopy(bigBytes, 1, smallerBytes, 0, smallerBytes.length); return smallerBytes; } }
The method simply performs toByteArray() if the bit length of the BigInteger is not a multiple of 8. In this case, the sign bit will not cause an extra byte to be added to the byte array. If the bit length is a multiple of 8, then the sign bit added by toByteArray() will cause an extra byte in the resulting byte array. In this case, getBytes() removes the extra byte and returns the result.