-
When I test my data to calculate CRC in below online calculator:
0x31 0x1c 0x4c 0x50 0x43
https://crccalc.com/?crc=0x31 0x1c 0x4c 0x50 0x43&method=CRC-16&datatype=hex&outtype=hexI get CRC-16/ARC = 0x72C6 . And it is a properly value.
When I try calculate this sum in Qt like below I have different output ?
QByteArray testData; testData.append(0x31); testData.append(0x1C); testData.append(0x4C); testData.append(0x50); testData.append(0x43); quint16 crc = calculateCrc(testData); quint16 calculateCrc(const QByteArray &data) { quint16 crc = 0x0000; // Init = 0 (ARC standard) for (char byte : data) { crc ^= (static_cast<quint8>(byte) << 8); // XOR high byte for (int i = 0; i < 8; i++) { crc = (crc & 0x8000) ? (crc << 1) ^ 0x8005 : (crc << 1); } } return crc; }
What is wrong ?
-
Hi,
I think you went the too complicated road:quint16 calculateCrc16Arc(const QByteArray &data) { quint16 crc = 0x0000; const quint16 poly = 0xA001; for (char byte : data) { crc ^= (static_cast<quint8>(byte)); for (int i = 0; i < 8; i++) { crc = (crc & 0x0001) > 0 ? (crc >> 1) ^ poly : (crc >> 1); } } return crc; }
-
You get 5C82 with the code above which looks like CRC16/UMTS according to the page you linked. So you are using the wrong algorithm, not related to Qt.
-
@Christian-Ehrlicher ,So what algorithm I should use ?
-
Hi,
I think you went the too complicated road:quint16 calculateCrc16Arc(const QByteArray &data) { quint16 crc = 0x0000; const quint16 poly = 0xA001; for (char byte : data) { crc ^= (static_cast<quint8>(byte)); for (int i = 0; i < 8; i++) { crc = (crc & 0x0001) > 0 ? (crc >> 1) ^ poly : (crc >> 1); } } return crc; }
-
S SGaist moved this topic from General and Desktop
-