2011-11-15 49 views
0

我想選擇字節數組中最後一個字節的低4位。這是我以前在PHP中完成的,但我是Java新手。Java字節數組操作

$lower4bit = substr($bytes[19], -1); 

//Convert the hex to decimal to get the offset value 
$offset = hexdec($lower4bit); 

//Select the value of the 4 bytes starting at the offset 
$joinedArray = implode(array_slice($bytes, $offset, 4)); 

任何人都可以用Java指出正確的方向嗎?

+0

我沒有時間得到完整的答案,但請記住......在PHP中,「數組」是數組,列表,地圖和其他所有東西。在Java中,它們並不是全部合而爲一。 – corsiKa

+0

即使在PHP中,這也是非常錯誤的做法。 '$ lower4bit = $ bytes [19] & 0x0F;' – Esailija

回答

3

您訪問數組像這樣:

y = a[i]; 

,您可以找到一個數組的長度,如下所示:

len = a.length; 

可以一個整數的最後4位隔離像這樣:

y = x & 0xF; 

這些應該足以構建您需要的代碼。

+0

謝謝。我遵循前兩個,那很棒。我現在有最後一個字節。不知道第三位是如何工作的。 0xF是什麼? – Joseph

+1

@Joseph:最後的聲明是應用[bitmask](http://en.wikipedia.org/wiki/Bitmask)。 「0xF」是一個十六進制常量,設置了4個lbsbs。 –