I have an android application which got data via serial port.
For example I got this byte array:
E8 FC 3E 0E 9C EB ED 0A 71 67 28 1C 28 28 02 00 75 8C 01 00 91 6A 00 00 4A 7C 00 00
I got this from a device, and in this documentation of the device I see this:
All multi-byte values are ordered in Little Endian format
ByteOffset: 0 - 4 and its an unsigned long
ByteOffset: 4 - 8 signed long
ByteOffset: 8 - 12 signed long
ByteOffset: 12 - 16 signed long etc.
My problem is that I can't convert it to a valid int or long.
I tried these convertings:
int val = (bytes[3] << (Byte.SIZE * 3));
val |= (bytes[2] & 0xFF) << (Byte.SIZE * 2);
val |= (bytes[1] & 0xFF) << (Byte.SIZE * 1);
val |= (bytes[0] & 0xFF);
long val = 0;
val += new Byte(bytes[3]).intValue();
val += new Byte(bytes[2]).intValue();
val += new Byte(bytes[1]).intValue();
val += new Byte(bytes[0]).intValue();
int val = java.nio.ByteBuffer.wrap(bytes).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
How should I parse my byte array?
UPDATE: Example input byte array:
B5 62 01 12 24 00 C0 C3 E3 01 03 00 00 00 01 00 00 00 09 00 00 00 0A 00 00 00 03 00 00 00 00 00 00 00 28 00 00 00 AA 64 FE 00 EC 8A
The first 2 bytes the header (B5 62)
The seconds 2 bytes the class and id (01 12)
After the 2 bytes the payload length (24 00)
The last 2 bytes the checksum (EC 8A)
Between the payload length and the checksum bytes I would like parse the data.
You can see on this picture the payload contents:
And here is the parsed data: