I'm using boosts crc CCITT. I need to enter the values for the crc calculation as I please, so the array size will differ, thats why I chose a dynamic array. But the thing is I get different results when I use the dynmic array, why is that? Or am I doing something wrong here?
unsigned char test[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
// returns 0x29B1 which is correct.
int dynsize;
std::wstring incoming ( L"313233343536373839" );
dynsize = ( incoming.size() / 2 ) + 1;
unsigned char* data = new unsigned char [dynsize];
data[0] = 0x31;
data[1] = 0x32;
data[2] = 0x33;
data[3] = 0x34;
data[4] = 0x35;
data[5] = 0x36;
data[6] = 0x37;
data[7] = 0x38;
data[8] = 0x39;
// that returns 0x5349 whis is not correct.
std::size_t const data_len = sizeof ( data ) / sizeof ( data[0] );
boost::crc_basic<16> crc_ccitt1( 0x1021 , 0xFFFF , 0 , false , false );
crc_ccitt1.process_bytes ( data , data_len );
Ofc later on the variable incoming
will get defined through input.
Solution:
std::size_t const data_len = sizeof ( data ) / sizeof ( data[0] ); // deleted
dynsize = ( incoming.size() / 2 );
crc_ccitt1.process_bytes ( data , dynsize );
sizeof(data) is a size of a pointer to unsigned char type. Therefor, this line always equals 4 (in x86 a size of a pointer is 4 bytes, and unsigned char is one):
data_len = 4 / 1;