Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two bytes in java/android

I have a frame of 22 bytes. The frame is the input stream from an accelerometer via bluetooth. The acceleromter readings are a 16 bit number split over two bytes.

When i try to merge the bytes with buffer[1] + buffer[2], rather than adding the bytes, it just puts the results side by side. so 1+2 = 12.

Could someone tell me how to combine these two bytes to obtain the original number. (btw the bytes are sent little endian)

Thanks

like image 736
Shane Avatar asked May 24 '10 10:05

Shane


People also ask

How do you merge bytes in Java?

byte[] one = getBytesForOne(); byte[] two = getBytesForTwo(); byte[] combined = new byte[one. length + two. length]; System. arraycopy(one,0,combined,0 ,one.


1 Answers

here's the code:

public static short twoBytesToShort(byte b1, byte b2) {
          return (short) ((b1 << 8) | (b2 & 0xFF));
}
like image 150
reflog Avatar answered Sep 23 '22 19:09

reflog