# How to use this CRC16 library?

I want to calculate the CRC-16/Xmodem from “0xDEADBEEF”.
The result should be 0xC457 as shown on this ewbsite:

I found this library that should be able to calculate it:
https://github.com/FrankBoesing/FastCRC

But with the following code i always get the result 0x45BA instead of my expected 0xC457.

What i am doing wrong?

``````#include <FastCRC.h>

FastCRC16 CRC16;

uint8_t buf = {'D', 'E', 'A', 'D', 'B', 'E', 'E', 'F'};

void setup() {

delay(1500);
Serial.begin(115200);

Serial.println("CRC Example");
Serial.println();

Serial.print("XMODEM-CRC of \"");

for (unsigned int i = 0; i < sizeof(buf); i++) {
Serial.print((char) buf[i]);
}

Serial.print("\" is: 0x");
Serial.println( CRC16.xmodem(buf, sizeof(buf)), HEX );

}

void loop() {
}
``````

DEADBEEF is the wrong example to be using when trying to understand how CRC calculations are derived. Why?
It can be read as a concatenation of the words dead and beef as ASCII characters, but note that 0xDEADBEEF is also a valid hex number.

As it turns out, CRC16 using XModem polynomial for DEADBEEF (ASCII) is 0x45BA and for 0xDEADBEEF (HEX) is 0xC457.

So, you're doing nothing wrong and the library is working fine. You just need to take care that you feed the correct data into the engine when comparing results. (I think DEADBEEF is meant to confuse, or at least to make people look at the obvious mistake it can lead to.)

Thank you DKWatson.

When i use hex-data it worked.

But i generally don't understand how to deal with hex data.

I have to reproduce this:
0x0610010e850330
What is similar to
uint8_t = {0x06,0x10,0x01,0x0e,0x85,0x03,0x30};

the first 3 bytes are always the same "0x06,0x10,0x01"
The next 2 bytes should be a value from an integer, as example '3477' what should be 0x0e85 in HEX
the last 2 bytes is the checksum i will calculate with the CRC-16/XMODEM.

I try for hours now but i don't understand how to convert my integer of '3477' to hex and add it to my buffer.

This is something i have in mind, but i know it's compleatly wrong. How to do it right?

``````uint8_t buffer; // setting up a buffer

uint8_t pre_data = {0x06,0x10,0x01}; //the first 3 bytes are always the same

int myvalue = 3477;
uint8_t hexvalue = (uint8_t) myvalue; // convert my value

uint8_t crcbuffer = {pre_data,pre_data,pre_data,hexvalue,hexvalue}; // combine the first 3 bytes with the converted value

uint8_t crcreturn = CRC16.xmodem(crcbuffer, sizeof(crcbuffer)) //calculate the CRC16 to a buffer

buffer[] = {crcbuffer,crcbuffer,crcbuffer,crcbuffer,crcbuffer,crcreturn,crcreturn};
``````

// combine all together

``````  uint8_t packet  = {0x06, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00};
unsigned int crcXmodem = CRC16.xmodem(packet, 5);
packet = highByte(crcXmodem);
packet = lowByte(crcXmodem);
``````

You're never really dealing with hex, or decimal for that matter. All data is binary, how it is displayed is another issue. If you examine data structures, each type is a number of bytes, typically 1, 2, 4 or 8. Each byte is comprised of 2 nibbles (4 bits) and each nibble can be represented by one hex digit.

So if you took 3477, in the computer it is likely stored in a 16-bit integer as 0000 1101 1001 0101. Now each of those 4-bit nibble can be converted however you like. As decimal they read 0 13 9 5. Convert then to hex it becomes 0D95. In C there is a convenient way to do this with a union and a bitfield struct.

``````union
{
uint16_t    value;
struct
{
uint8_t    nibbleA: 4;
uint8_t    nibbleB: 4;
uint8_t    nibbleC: 4;
uint8_t    nibbleD: 4;
};
} int2nib;
``````

Now when you assign int2nib.value = 3477, it is stored in the union as nibbleD nibbleC nibbleB nibbleA or 0000 1101 1001 0101. These values can the be used anyway you like. If you want to print them as hex, use them as an index to a char array,

``````    char d2h = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
printf("%c\t",d2h[int2nib.nibbleD]);
printf("%c\t",d2h[int2nib.nibbleC]);
printf("%c\t",d2h[int2nib.nibbleB]);
printf("%c\n",d2h[int2nib.nibbleA]);
``````

Or use one of many built in functions.