Hallo zusammen,
ich habe ein problem beim CRC wert berechnen. ich habe zwar in Internet verschieden codes gefunden..habe auch was geschrieben aber das ergebnins stimmt nicht!!!!!???????
ergebnis sollte D0BD sein für den wert 180007D202580258
ich habe ein problem beim CRC wert berechnen. ich habe zwar in Internet verschieden codes gefunden..habe auch was geschrieben aber das ergebnins stimmt nicht!!!!!???????
Code:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytesArray = new byte[] { 0x18,0x00,0x07,0xD2,0x02,0x58,0x02,0x58 };
Crc16 crcclass = new Crc16();
textBox1.Text = String.Format("{0}", crcclass.ComputeChecksum(bytesArray));
byte[] result = crcclass.ComputeChecksumBytes(bytesArray);
textBox2.Text = "";
foreach (byte bit in result)
{
textBox2.Text += String.Format("{0:X}", bit); // Hexawert ausgabe
textBox2.Text += Convert.ToString(1,2); //binär umwandlung
}
}
}
public class Crc16
{
const int polynomial = 0x14EAB;
int[] table = new int[256];
public int ComputeChecksum(byte[] bytes)
{
int crc = 0;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(crc ^ bytes[i]);
crc = (int)((crc >> 8) ^ table[index]);
}
return crc;
}
public byte[] ComputeChecksumBytes(byte[] bytes)
{
int crc = ComputeChecksum(bytes);
return BitConverter.GetBytes(crc);
}
public Crc16()
{
int value;
int temp;
for (int i = 0; i < table.Length; ++i)
{
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j)
{
if (((value ^ temp) & 0x0001) != 0)
{
value = (int)((value >> 1) ^ polynomial);
}
else
{
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}
}
ergebnis sollte D0BD sein für den wert 180007D202580258
Zuletzt bearbeitet: