CRC8 (uint8_t) from uint16_t data

Hello, i m new to c++ and low level programming and i need to check uint16 data with an crc with uint8 check sum i only have 3 free dec places for the crc to transfer - max 999(0x3E7).

So i read now some about crc and i guess what i need is a crc8, does anyone knows an algorithm that can fit my purpose or i am taking it the wrong way?


How can i for example modify this algorithm to be able to check uint16_t and not only uint8_t messages without making it useless? (its from https://barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #define POLYNOMIAL 0xD8  /* 11011 followed by 0's */

uint8_t
crcNaive(uint8_t const message)
{
    uint8_t  remainder;	


    /*
     * Initially, the dividend is the remainder.
     */
    remainder = message;

    /*
     * For each bit position in the message....
     */
    for (uint8_t bit = 8; bit > 0; --bit)
    {
        /*
         * If the uppermost bit is a 1...
         */
        if (remainder & 0x80)
        {
            /*
             * XOR the previous remainder with the divisor.
             */
            remainder ^= POLYNOMIAL;
        }

        /*
         * Shift the next bit of the message into the remainder.
         */
        remainder = (remainder << 1);
    }

    /*
     * Return only the relevant bits of the remainder as CRC.
     */
    return (remainder >> 4);

}  


Thanks a lot.
Last edited on
I would not modify it.
instead, force-fit the 16 bit data into 8 bit data, CRC that.

eg
uint16_t data[1000];
...
crc = crcNaive( (uint8_t*)(data) );

That is, just crc *everything* that needs it as if it were a byte stream, and it will work on everything. You *can* write a 16 bit crc but it won't work on generic data.
Last edited on
Topic archived. No new replies allowed.