Hex:[0A][52][08][01][01][01][00][CD][21][02][59]
0 [0A]
1 [52] Packettype = TEMP_HUM
2 [08] subtype = TH8 -
3 [01] Sequence nbr = 1
4/5 [01][01] ID = 257
6/7 [00][CD] Temperature = 20.5 °C
8 [21] Humidity = 33
9 [02] Status = Dry
10 [5] *nibble Signal level = 5
11 [9] *nibble Battery = OK
因此,我通過串口獲得了11個字節(十六進制)。 我將所有字節分配給一個字節數組,以便稍後使用它們。從Java字節中提取半字節
我有兩個qestions:
1]我如何能在4 & 5個字節組合到一起在Java中(我假設在INT)? 2]如何提取10和11或最後一個字節的高位和低位?
[FROM COMMENTS下文] 例字節:高[0101] [0110]低 lowNibble = yourbyte &爲0x0F; 爲0x0F(00001111)= 00000110
highNibble = yourByte >>>> 4
01010110 >>> 4 = 00000101
IF you use this Example Byte: High[1101][0110]Low
highNibble = yourByte >>> 4
11010110 >>> 4 = 00000101
Because >>> removes the signed bit.
其實我只知道如何將簽名字節變成無符號字節int anUnsignedByte =(int)b & 0xff;但我真的不知道發生了什麼。 – DevilCode
是的。請注意它是>>>而不是>>>> – Eduardo