2011-10-09 91 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字節順序:最重要的字節位於第零個元素中。