2

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: enter image description here

I4 and U4 means: enter image description here enter image description here

And here is the parsed data:

enter image description here

just
  • 1,900
  • 4
  • 25
  • 46

1 Answers1

0

Looks like a nice puzzle. But I don't think I can help you much. Alas.

When I put in your list, it looks like Hex, not byes;

int [] hexes = {0xB5, 0x62, 0x01, 0x12, 0x24, 0x00, 0xC0, 0xC3, 0xE3, 0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0xAA, 0x64, 0xFE, 0x00, 0xEC, 0x8A};

Google-fu: Converting Little Endian to Big Endian

Resources:

Edianness

Java Hex

Java & Endian Java Integer are Big Endian.

Community
  • 1
  • 1