Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Problem with calculating CRC
Forum Updated to NodeBB v4.3 + New Features

Problem with calculating CRC

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 341 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Damian7546
    wrote last edited by
    #1

    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=hex

    I 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 ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #4

      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;
      }
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote last edited by
        #2

        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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Damian7546
          wrote last edited by
          #3

          @Christian-Ehrlicher ,So what algorithm I should use ?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote last edited by
            #4

            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;
            }
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • SGaistS SGaist moved this topic from General and Desktop
            • D Damian7546 has marked this topic as solved

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved