I'm reading (binary) data from file and generating the CRC16 checksum. The next step is to write this checksum at the end of this file (as last 2 bytes) and then calculate CRC16 again which should be 0. The problem is the CRC which i wrote to the file is different. For example i use write(reinterpret_cast<const char *>(&crc), 2)
to writeshort int crc = 0xba10
at the end, but in fact im writing ş
which is
00010000 11000101 10011111
not 10111010 00010000
.Is there any way to write it properly at the end of file?
Here is my crc calculating code:
int crc16(char* data_p, int length){
unsigned char x;
unsigned short crc = 0x1D0F;
while (length--){
x = crc >> 8 ^ *data_p++;
x ^= x >> 4; // x = x ^ (x >> 4)
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
}
return crc;
}