2011-10-09 87 views

回答

3

首先,确保您知道您的int[]是以何种格式进行解释。

每个int可以看作由四个字节组成,并且这些字节可以一起转换为BigInteger。细节是字节顺序 - 哪一个字节最重要,哪一个最不重要?

此外,你有一个有符号或无符号数字?

一种简单的方法你int秒值进行转换,以byte s(对于在一个BigInteger构造后使用)是使用ByteBuffer和包装一个IntBuffer周围。

public BigInteger toBigInteger(int[] data) { 
    byte[] array = new byte[data.length * 4]; 
    ByteBuffer bbuf = ByteBuffer.wrap(array); 
    IntBuffer ibuf = bbuf.asIntBuffer(); 
    ibuf.put(data); 
    return new BigInteger(array); 
} 

明显adaptions是设置的字节顺序的bbuf,或使用其他的BigInteger构造(无符号)。

2

那么new BigInteger(byte[] val)呢?

引述我链接到API文档:

平移含有一个字节数组二进制补码的BigInteger的二进制表示转换为BigInteger。输入数组假定为big-endian字节顺序:最重要的字节位于第零个元素中。