Übersetzen CRC8 von C nach Java

Erhielt ich ein Stück code in C, die berechnet eine CRC8-Wert, der ein byte-array.
Ich brauche, um es zu übersetzen in Java.

Hier die C Code:

CRC_POLYNOM = 0x9c;
CRC_PRESET = 0xFF;

unsigned int CRC = CRC_PRESET;
for (i = 0; i < Len; i++)
{
  crc ^= FRAME[i];
  for (j = 0; j < 8; j++)
  {
    if (crc & 0x01)
        crc = (crc >> 1) ^ CRC_POLYNOM;
    else
        crc = (crc >> 1);
  }
}

Was ich haben es geschafft, zu tun ist, dies in Java:

public static long calculateCRC8(byte[] b, int len) {
  long crc = CRC_PRESET;
  for (int i = 0; i < len; i++) {
    crc ^= b[i];
    for (int j = 0; j < 8; j++) {
      if ((crc & 0x01) == 0)
        crc = (crc >> 1) ^ CRC_POLYNOM;
      else
        crc = crc >> 1;
    }
  }
return crc;
}

Ein Beispiel für einen byte-array:

byte[] b = new byte[] {1, 56, -23, 3, 0, 19, 0, 0, 2, 0, 3, 13, 8, -34, 7, 9, 42, 18, 26, -5, 54, 11, -94, -46, -128, 4, 48, 52, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 1, -32, -80, 0, 98, -5, 71, 0, 64, 0, 0, 0, 0, -116, 1, 104, 2};

den C code gibt 29, meine Java code gibt 44 obwohl.
Was habe ich falsch gemacht?

Ich denke, das ist, weil der Java signed-only-Datentypen, so wie ich das beheben kann?

Schreibe einen Kommentar