I have the following code in C# to calculate a CRC and it works like I want it to.
public byte crc_8(byte[] byteArray)
{
ushort reg_crc = 0;
for(int i = 0; i<byteArray.Length; i++)
{
reg_crc ^= byteArray[i];
for(int j = 0; j < 8; j++)
{
if((reg_crc & 0x01) == 1)
{
reg_crc = (ushort)((reg_crc >> 1) ^ 0xE5);
}
else
{
reg_crc = (ushort)(reg_crc >> 1);
}
}
}
reg_crc = (byte)(reg_crc & 0xFF);
return (byte)reg_crc;
}
I also need to add this same function to a code project that is in C++, but I am brand new to C++. This is as far as I have gotten, and I am not sure with how to proceed with the code inside of the for loop. Also note that RX_PACKET_SIZE
is equivalent to byteArray.Length
in that it can be used for the same purpose. I know that is okay.
static uint8_t crc_8(unit8_t array_to_process [])
{
uint16_t reg_crc = 0;
for(int i = 0; i < RX_PACKET_SIZE; i++)
{
}
}