/**
* 逆轉(zhuǎn)字節(jié)數(shù)組
*
* @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;
}
/**
* 讀取無(wú)符號(hào)位的Short數(shù),16位
*
* @param readBuffer
* @return
* @throws IOException
*/
public static final BigInteger readUnsignedShort(byte[] readBuffer)
throws IOException {
if (readBuffer == null || readBuffer.length < 2)
return new BigInteger("0");
// 處理成無(wú)符號(hào)數(shù)
byte[] uint64 = new byte[3];
uint64[2] = 0;
System.arraycopy(readBuffer, 0, uint64, 0, 2);
return new BigInteger(reverse(uint64));
}
/**
* 讀取無(wú)符號(hào)位的長(zhǎng)整數(shù),64位
*
* @param readBuffer
* @return
* @throws IOException
*/
public static final BigInteger readUnsignedInt64(byte[] readBuffer)
throws IOException {
if (readBuffer == null || readBuffer.length < 8)
return new BigInteger("0");
// 處理成無(wú)符號(hào)數(shù)
byte[] uint64 = new byte[9];
uint64[8] = 0;
System.arraycopy(readBuffer, 0, uint64, 0, 8);
return new BigInteger(reverse(uint64));
}
* 逆轉(zhuǎn)字節(jié)數(shù)組
*
* @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;
}
/**
* 讀取無(wú)符號(hào)位的Short數(shù),16位
*
* @param readBuffer
* @return
* @throws IOException
*/
public static final BigInteger readUnsignedShort(byte[] readBuffer)
throws IOException {
if (readBuffer == null || readBuffer.length < 2)
return new BigInteger("0");
// 處理成無(wú)符號(hào)數(shù)
byte[] uint64 = new byte[3];
uint64[2] = 0;
System.arraycopy(readBuffer, 0, uint64, 0, 2);
return new BigInteger(reverse(uint64));
}
/**
* 讀取無(wú)符號(hào)位的長(zhǎng)整數(shù),64位
*
* @param readBuffer
* @return
* @throws IOException
*/
public static final BigInteger readUnsignedInt64(byte[] readBuffer)
throws IOException {
if (readBuffer == null || readBuffer.length < 8)
return new BigInteger("0");
// 處理成無(wú)符號(hào)數(shù)
byte[] uint64 = new byte[9];
uint64[8] = 0;
System.arraycopy(readBuffer, 0, uint64, 0, 8);
return new BigInteger(reverse(uint64));
}
---------------------------------------------------------
專注移動(dòng)開(kāi)發(fā)
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian