無符號數都可以用BigInteger來完成表現,他自身附帶了很多計算方法,可以有效地完成無符號數的計算工作。
/**
* 逆轉字節數組
*
* @param b
* @return
*/
private static byte[] reverse(byte[] b) {
byte[] temp = new byte[b.length];
for (int i = 0; i < b.length; i++) {
temp[i] = b[b.length - 1 - i];
}
return temp;
}
/**
* 讀取無符號位的Short數,16位
*
* @param readBuffer
* @return
* @throws IOException
*/
private static final BigInteger readUnsignedShort(byte[] readBuffer)
throws IOException {
if (readBuffer == null || readBuffer.length < 2)
return new BigInteger("0");
// 處理成無符號數
byte[] uint64 = new byte[3];
uint64[2] = 0;
System.arraycopy(readBuffer, 0, uint64, 0, 2);
return new BigInteger(reverse(uint64));
}
/**
* 讀取無符號位的長整數,64位
*
* @param readBuffer
* @return
* @throws IOException
*/
private static final BigInteger readUnsignedInt64(byte[] readBuffer)
throws IOException {
if (readBuffer == null || readBuffer.length < 8)
return new BigInteger("0");
// 處理成無符號數
byte[] uint64 = new byte[9];
uint64[8] = 0;
System.arraycopy(readBuffer, 0, uint64, 0, 8);
return new BigInteger(reverse(uint64));
}
---------------------------------------------------------
專注移動開發
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2010-05-03 09:18
TiGERTiAN 閱讀(9122)
評論(0) 編輯 收藏 所屬分類:
Java