Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate CRC8 from C to Java

Tags:

java

c

crc

I received a piece of code in C which calculates a CRC8-value of a byte array. I need to translate it to Java.

Here the 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);
  }
}

What I have managed to do is this 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;
}

For a sample 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};

the C code returns 29, my Java code returns 44 though. What did I do wrong?

I think this is because of Java's signed-only datatypes, so how can I fix this?

like image 600
Alexander Ciesielski Avatar asked Mar 03 '26 16:03

Alexander Ciesielski


1 Answers

if (crc & 0x01)

This tests if the lowest bit is set.

if ((crc & 0x01) == 0)

This tests if the bottom bit is clear.

You should also use an unsigned right shift in the Java code (i.e. >>> instead of >>, in two places), and mask the result with 0xff before returning it.

EDIT And finally you need to change this:

crc ^= b[i];

to this:

crc ^= b[i] & 0xff;

However you should really throw it all away and look up the table driven method. It is eight times as fast as this.

EDIT 2 Table-driven version, revised so as to implement java.util.zip.Checksum:

public class CRC8 implements Checksum
{
    private final short init;
    private final short[]   crcTable = new short[256];
    private short   value;

    /**
     * Construct a CRC8 specifying the polynomial and initial value.
     * @param polynomial Polynomial, typically one of the POLYNOMIAL_* constants.
     * @param init Initial value, typically either 0xff or zero.
     */
    public CRC8(int polynomial, short init)
    {
        this.value = this.init = init;
        for (int dividend = 0; dividend < 256; dividend++)
        {
            int remainder = dividend ;//<< 8;
            for (int bit = 0; bit < 8; ++bit)
                if ((remainder & 0x01) != 0)
                    remainder = (remainder >>> 1) ^ polynomial;
                else
                    remainder >>>= 1;
            crcTable[dividend] = (short)remainder;
        }
    }

    @Override
    public void update(byte[] buffer, int offset, int len)
    {
        for (int i = 0; i < len; i++)
        {
            int data = buffer[offset+i] ^ value;
            value = (short)(crcTable[data & 0xff] ^ (value << 8));
        }
    }

    /**
     * Updates the current checksum with the specified array of bytes.
     * Equivalent to calling <code>update(buffer, 0, buffer.length)</code>.
     * @param buffer the byte array to update the checksum with
     */
    public void update(byte[] buffer)
    {
        update(buffer, 0, buffer.length);
    }

    @Override
    public void update(int b)
    {
        update(new byte[]{(byte)b}, 0, 1);
    }

    @Override
    public long getValue()
    {
        return value & 0xff;
    }

    @Override
    public void reset()
    {
        value = init;
    }

    public static void  main(String[] args)
    {
        final int   CRC_POLYNOM = 0x9C;
        final byte  CRC_INITIAL = (byte)0xFF;

        final byte[]    data = {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};
        CRC8    crc8 = new CRC8(CRC_POLYNOM, CRC_INITIAL);
        crc8.update(data,0,data.length);
        System.out.println("Test successful:\t"+(crc8.getValue() == 29));
    }
}
like image 159
user207421 Avatar answered Mar 06 '26 07:03

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!