ISO/IEC13239 CRC16 Implementation



我需要一个用于NFC标签的CRC16实现。正如标准告诉我的那样,这是ISO/IEC13239,并提供了一个示例C代码。我把这段代码翻译成了Java,但它给了我错误的结果:

private static final char POLYNOMIAL = 0x8404;
private static final char PRESET_VALUE = 0xFFFF;
public static int crc16(byte[] data) {
char current_crc_value = PRESET_VALUE;
for (int i = 0; i < data.length; i++) {
    current_crc_value = (char) (current_crc_value ^ ((char) data[i]));
    for (int j = 0; j < 8; j++) {
    if ((current_crc_value & 0x0001) == 0x0001) {
        current_crc_value = (char) ((current_crc_value >>> 1) ^ POLYNOMIAL);
    } else {
        current_crc_value = (char) (current_crc_value >>> 1);
    }
    }
}
current_crc_value = (char) ~current_crc_value;
return current_crc_value;
}

正如标准告诉我的那样,1,2,3,4的字节序列应该创建0x3991的CRC值A C版本见第42页:http://www.waazaa.org/download/fcd-15693-3.pdf

其他CRC实现也不起作用:crc16实现java第一个给我0x9e33,第二个0x0FA1(顺便说一下,我的实现是0xE1E5

是否有人在我的样本中发现错误,或者是否有其他CRC16实现真正有效?

你的答案很接近,但我认为掩码和多项式可能存在一些问题。以下是一些似乎对我有效的调整:

private static final int POLYNOMIAL   = 0x8408;
private static final int PRESET_VALUE = 0xFFFF;
public static int crc16(byte[] data)
{
  int current_crc_value = PRESET_VALUE;
  for (int i = 0; i < data.length; i++ )
  {
    current_crc_value ^= data[i] & 0xFF;
    for (int j = 0; j < 8; j++)
    {
      if ((current_crc_value & 1) != 0)
      {
        current_crc_value = (current_crc_value >>> 1) ^ POLYNOMIAL;
      }
      else
      {
        current_crc_value = current_crc_value >>> 1;
      }
    }
  }
  current_crc_value = ~current_crc_value;
  return current_crc_value & 0xFFFF;
}

首先,PDF有:

#define POLYNOMIAL 0x8408 // x^16 + x^12 + x^5 + 1

当你有时

private static final char POLYNOMIAL = 0x8404;

这肯定会引起问题——请解决这个问题,并让我们知道这是否是问题所在。

我有点担心他们说0x8408等价于x^16 + x^12 + x^5 + 1,因为它不是,多项式将由0x8811表示。CCD_ 9将表示CCD_。就这一点而言,0x8404也不是。

这里有一个更快的实现(源代码)。

使用初始值为0xFFFF的ccittPoly并对结果进行补码。这为new byte[]{1, 2, 3, 4}提供了0x3991。

public class Crc16 {
// Generator polynom codes:
public static final int stdPoly    = 0xA001; // standard CRC-16 x16+x15+x2+1 (CRC-16-IBM)
public static final int stdRPoly   = 0xC002; // standard reverse x16+x14+x+1 (CRC-16-IBM)
public static final int ccittPoly  = 0x8408; // CCITT/SDLC/HDLC X16+X12+X5+1 (CRC-16-CCITT)
   // The initial CRC value is usually 0xFFFF and the result is complemented.
public static final int ccittRPoly = 0x8810; // CCITT reverse X16+X11+X4+1   (CRC-16-CCITT)
public static final int lrcPoly    = 0x8000; // LRCC-16 X16+1
private short[] crcTable;
public Crc16 (int polynom) {
   crcTable = genCrc16Table(polynom); }
public int calculate (byte[] data, int initialCrcValue) {
   int crc = initialCrcValue;
   for (int p = 0; p < data.length; p++) {
      crc = (crc >> 8) ^ (crcTable[(crc & 0xFF) ^ (data[p] & 0xFF)] & 0xFFFF); }
   return crc; }
private static short[] genCrc16Table (int polynom) {
   short[] table = new short[256];
   for (int x = 0; x < 256; x++) {
      int w = x;
      for (int i = 0; i < 8; i++) {
         if ((w & 1) != 0) {
            w = (w >> 1) ^ polynom; }
          else {
            w = w >> 1; }}
      table[x] = (short)w; }
   return table; }
}

最新更新