Domanda

Come calcolare la codifica CRC_B in C # come descritto nella ISO 14443? Ecco alcune informazioni di base:

  

Codifica CRC_B   Questo allegato è fornito a scopo esplicativo e indica i modelli di bit che lo faranno   esiste nel livello fisico. È incluso allo scopo di verificare una ISO / IEC   14443-3 Implementazione di tipo B della codifica CRC_B. Fare riferimento a ISO / IEC 3309 e CCITT X.25   2.2.7 e V.42 8.1.1.6.1 per ulteriori dettagli. Valore iniziale = 'FFFF'

  • Esempio 1: per 0x00 0x00 0x00 dovresti finire con CRC_B di 0xCC 0xC6
  • Esempio 2: per 0x0F 0xAA 0xFF dovresti finire con CRC_B di 0xFC 0xD1

Ho provato alcune librerie CRC16 casuali ma non mi danno gli stessi risultati. Non ho ottenuto gli stessi risultati dai controlli online come in qui .

È stato utile?

Soluzione

L'ho invertito dal codice C in ISO / IEC JTC1 / SC17 N 3497 non è carino ma fa quello che ti serve:

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;
    }
}

Come test, prova il codice seguente:

 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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top