2
IAM從c.dot網的Web服務越來越轉換字節[]爲長黑莓
byte[] data = new byte[] {-33, -96,0, 0, 0,0,0,0};
我想這個轉換爲長期價值 我想這
long result = (long)ByteBuffer.wrap(index).getInt();
我得到結果爲-543162368
其實際值爲41183
IAM從c.dot網的Web服務越來越轉換字節[]爲長黑莓
byte[] data = new byte[] {-33, -96,0, 0, 0,0,0,0};
我想這個轉換爲長期價值 我想這
long result = (long)ByteBuffer.wrap(index).getInt();
我得到結果爲-543162368
其實際值爲41183
首先你想要t o在緩衝區上調用getLong()
而不是getInt()
。
但是,您收到的數據是little-endian,這意味着它首先從低位字節開始。 ByteBuffers
被構造爲具有大端順序的默認值。您需要將訂單設置爲LITTLE_ENDIAN
以獲取正確的值。
ByteBuffer buffer = ByteBuffer.wrap(index)
buffer.order(ByteOrder.LITTLE_ENDIAN);
long result = buffer.getLong();
既然你顯然不能設置的字節順序或使用getLong,你需要做的是這樣的:
// Reverse array
for (int i = 0; i < 4; ++i)
{
byte temp = data[i];
data[i] = data[8-i];
data[8-i] = temp;
}
// Get two ints and shift the first int into the high order bytes
// of the result.
ByteBuffer buffer = ByteBuffer.wrap(data);
long result = ((long)buffer.getInt()) << 32;
result |= (long)buffer.getInt();
result
現在應該包含的值。
但黑莓手機不支持getLong(),黑莓手機也不支持ByteOrder – user1203673
手動反轉該數組,然後調用get int兩次,將第一個結果強制轉換爲long並將其移動32位,並將其與第二次通話的結果。 – Dervall
我按照你建議的先生德沃爾,但亞姆得到的結果爲零,但我試過這樣,並得到結果長l = 0; (int i = 0; i <8; i ++){ \t \t \t \t \t L^=(長)字節[I] & 0xff; \t \t} \t \t返回升; – user1203673