Solved by this code -> https://gist.github.com/Sbreitzke/b26107798eee74e39ff85800abf71fb1
I searched the internet for implementing CRC 4 in C # because I need to calculate the checksum
Change the barcode numbers in hexadecimal notation, then bytes and then bits, and then calculate the CRC4 checksum in the bit stream.
I already found this question 8 years ago with no CRC-4 answer
in C # .
I tried changing CRC 8 and 16 implementations to CRC 4, but they do not quite get the desired result.
0130E0928270FFFFFFF
must be rated before 7
.
I found two implementations of C, but could not convert them to C #. For example, this one:
short[] crc4_tab = {
0x0, 0x7, 0xe, 0x9, 0xb, 0xc, 0x5, 0x2,
0x1, 0x6, 0xf, 0x8, 0xa, 0xd, 0x4, 0x3,
};
short crc4(uint8_t c, uint64_t x, int bits)
{
int i;
x &= (1ull << bits) -1;
bits = (bits + 3) & ~0x3;
for (i = bits - 4; i >= 0; i -= 4)
c = crc4_tab[c ^ ((x >> i) & 0xf)];
return c;
}
My current generation code (unit test) is as follows:
[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);
}
short[] crc4_tab = {
0x0, 0x7, 0xe, 0x9, 0xb, 0xc, 0x5, 0x2,
0x1, 0x6, 0xf, 0x8, 0xa, 0xd, 0x4, 0x3,
};
short crc4(byte c, ulong x, int bits)
{
int i;
x &= ((ulong)1 << bits) -1;
bits = (bits + 3) & ~0x3;
for (i = bits - 4; i >= 0; i -= 4)
c = (byte) crc4_tab[c ^ ((x >> i) & 0xf)];
return c;
}