如何按照 ISO 14443 中的描述在 C# 中计算 CRC_B 编码?以下是一些背景信息:

编码此附件的CRC_B用于解释性目的,并指示物理层中将存在的位模式。为了检查ISO/IEC 14443-3型BC_B编码的B型实现。有关更多详细信息,请参阅ISO/IEC 3309和CCITT X.25 2.2.7和V.42 8.1.1.6.1。初始值 = 'FFFF'

  • 示例1:对于 0x00 0x00 0x00 你应该得到 0xCC 0xC6 的 CRC_B
  • 示例2:对于 0x0F 0xAA 0xFF 你应该得到 0xFC 0xD1 的 CRC_B

我尝试了一些随机的 CRC16 库,但它们没有给我相同的结果。我也没有从在线检查中得到相同的结果 这里.

有帮助吗?

解决方案

我从 C 代码中反转了这一点 ISO/IEC JTC1/SC17 N 3497 所以它不漂亮,但可以满足您的需要:

public class CrcB
{
    const ushort __crcBDefault = 0xffff;

    private static ushort UpdateCrc(byte b, ushort crc)
    {
            unchecked
            {
                byte ch = (byte)(b^(byte)(crc & 0x00ff));
                ch = (byte)(ch ^ (ch << 4));
                return (ushort)((crc >> 8)^(ch << 8)^(ch << 3)^(ch >> 4));
            }
    }

    public static ushort ComputeCrc(byte[] bytes)
    {
            var res = __crcBDefault;
            foreach (var b in bytes)
                    res = UpdateCrc(b, res);
            return (ushort)~res;
    }
}

作为测试,请尝试以下代码:

 public static void Main(string[] args) 
 {
     // test case 1 0xFC, 0xD1
     var bytes = new byte[] { 0x0F, 0xAA, 0xFF };
     var crc = CrcB.ComputeCrc(bytes);
     var cbytes = BitConverter.GetBytes(crc);

     Console.WriteLine("First (0xFC): {0:X}\tSecond (0xD1): {1:X}", cbytes[0], cbytes[1]);

     // test case 2 0xCC, 0xC6
     bytes = new byte[] { 0x00, 0x00, 0x00 };
     crc = CrcB.ComputeCrc(bytes);
     cbytes = BitConverter.GetBytes(crc);
     Console.WriteLine("First (0xCC): {0:X}\tSecond (0xC6): {1:X}", cbytes[0], cbytes[1]);


     Console.ReadLine();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top