I am experimenting with BigIntegers with random values.
BigInteger bi1 = new BigInteger("365375409332725729550921208179070754913983135742");
BigInteger bi2 = new BigInteger("386718480727050819604290866521372728863001258687");
byte[] bytesBi1 = bi1.toByteArray();
byte[] bytesBi2 = bi2.toByteArray();
The first one is the one which produces a strange value.
The second is ok.
Can someone explain to me what I am doing wrong ?
Edit
Here’re the results:
bytesBi1 = [63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2]
bytesBi2 = [67, -67, 14, 113, -94, 16, -120, 82, 84, 95, -127, -124, -90, -33, 29, -102, 90, 125, -86, -65]
BigInteger‘stoByteArray()method returns the two’s complement representation of the number.The values you’re getting are in fact the correct two’s complement representation of the numbers you put in.