gpt4 book ai didi

c# - C# 的 CRC 4 实现

转载 作者:太空狗 更新时间:2023-10-30 00:37:40 26 4
gpt4 key购买 nike

通过此代码解决 -> 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;
}

关于c# - C# 的 CRC 4 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46971887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com