C# 的 CRC 4 实现

CRC 4 implementation for C#

通过此代码解决 -> https://gist.github.com/Sbreitzke/b26107798eee74e39ff85800abf71fb1


我在网上搜索了 C# 中的 CRC 4 实现,因为我必须通过

计算校验和

Changing the numbers of the barcode into Hex representation, then to bytes and then to bits and then calculate a CRC4 checksum on the bit stream.

这个8年前的问题我已经找到了没有答案 CRC-4 implementation in C#.

我尝试将 CRC 8 和 16 实现更改为 CRC 4,但它们并没有完全得到我需要的结果。

0130E0928270FFFFFFF 应评估为 7.

我找到了两个 C 实现,但无法将它们转换为 C#。例如这个:

 short[] crc4_tab = {
 0x0, 0x7, 0xe, 0x9, 0xb, 0xc, 0x5, 0x2,
 0x1, 0x6, 0xf, 0x8, 0xa, 0xd, 0x4, 0x3,
};

/**
* crc4 - calculate the 4-bit crc of a value.
* @crc:  starting crc4
* @x:    value to checksum
* @bits: number of bits in @x to checksum
*
* Returns the crc4 value of @x, using polynomial 0b10111.
*
* The @x value is treated as left-aligned, and bits above @bits are ignored
* in the crc calculations.
*/
short crc4(uint8_t c, uint64_t x, int bits)
{
    int i;

    /* mask off anything above the top bit */
    x &= (1ull << bits) -1;

    /* Align to 4-bits */
    bits = (bits + 3) & ~0x3;

    /* Calculate crc4 over four-bit nibbles, starting at the MSbit */
    for (i = bits - 4; i >= 0; i -= 4)
    c = crc4_tab[c ^ ((x >> i) & 0xf)];

    return c;
}

我当前生成的代码(单元测试)如下所示:

[TestMethod]
public void x()
{
    var ordnungskennzeichen = 01;
    var kundennummer = 51251496;
    var einlieferungsbel = 9999;
    var sendungsnr = 16777215;

    var hex_ordnungskennzeichen = ordnungskennzeichen.ToString("x2");
    var hex_kundennummer = kundennummer.ToString("x2");
    var hex_einlieferungsbel = einlieferungsbel.ToString("x2");
    var hex_sendungsnr = sendungsnr.ToString("x2");

    var complete = hex_ordnungskennzeichen + hex_kundennummer + hex_einlieferungsbel + hex_sendungsnr;

    var bytes = Encoding.ASCII.GetBytes(complete);

    //var computeChecksum = crc4(???);
    //   Console.WriteLine(computeChecksum);

}

short[] crc4_tab = {
    0x0, 0x7, 0xe, 0x9, 0xb, 0xc, 0x5, 0x2,
    0x1, 0x6, 0xf, 0x8, 0xa, 0xd, 0x4, 0x3,
};

/**
* crc4 - calculate the 4-bit crc of a value.
* @crc:  starting crc4
* @x:    value to checksum
* @bits: number of bits in @x to checksum
*
* Returns the crc4 value of @x, using polynomial 0b10111.
*
* The @x value is treated as left-aligned, and bits above @bits are ignored
* in the crc calculations.
*/

short crc4(byte c, ulong x, int bits)
{
    int i;

    /* mask off anything above the top bit */
    x &= ((ulong)1 << bits) -1;

    /* Align to 4-bits */
    bits = (bits + 3) & ~0x3;

    /* Calculate crc4 over four-bit nibbles, starting at the MSbit */
    for (i = bits - 4; i >= 0; i -= 4)
        c = (byte) crc4_tab[c ^ ((x >> i) & 0xf)];

    return c;
}

将其转换为 C# 并不难。 c 是初始或前一个半字节(4 位数字),x 是要计算 crc4 的 64 位数字,bits 是实际使用的 64 位数字中的位数(其余的被忽略)。因为你有字节数组 - 你不需要使用 64 位数字作为 x - 使用可以只使用字节。然后前两行与您无关,因为它们所做的只是从 64 位数字中丢弃不相关的位并确保 bits 可被 4 整除。因此在删除不相关的行后,您的实现变为:

static readonly byte[] crc4_tab = {
    0x0, 0x7, 0xe, 0x9, 0xb, 0xc, 0x5, 0x2,
    0x1, 0x6, 0xf, 0x8, 0xa, 0xd, 0x4, 0x3,
};

static byte crc4(byte c, byte x) {
    var low4Bits = x & 0x0F;
    var high4Bits = x >> 4;
    c = crc4_tab[c ^ high4Bits];
    c = crc4_tab[c ^ low4Bits];

    return c;
}

static byte crc4(byte[] array) {
    byte start = 0;
    foreach (var item in array) {
        start = crc4(start, item);
    }
    return start;
}

经过进一步测试并与 Deutsche Post AG 沟通后,我们做出了正确的实施(至少为了 Deutsche Post 的目的):

https://gist.github.com/Sbreitzke/b26107798eee74e39ff85800abf71fb1

同样为了 Deutsche Post 的目的,我想贡献一个不太复杂的算法,它也可以更容易地翻译成其他语言:

       private string crc4(string sText) {
        int iCRC;
        int iPoly;
        int iByte;
        int iBit;
        byte[] bText;
        sText = sText.Replace(" ", "");
        iPoly = 0x13 << 3;
        iCRC = 0;
        bText = Encoding.Default.GetBytes(sText);
        for (iByte=0; iByte < bText.Length; iByte++){
            iCRC = iCRC ^ bText[iByte];
            for (iBit = 0; iBit < 8; iBit++){
                if ((iCRC & 0x80) != 0){
                    iCRC = iCRC ^ iPoly;
                }
                iCRC = iCRC << 1;
            }
        }
        iCRC = iCRC >> 4;

        return String.Format("{0:X}", iCRC);
    }

输入 "A0 0101 002B 00 000C D10" 上面的代码将计算 "F" 作为正确的校验位。 (并使用许多其他输入值进行测试)