Results 1 to 3 of 3

Thread: CRC-16 calculaion with qChecksum

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: CRC-16 calculaion with qChecksum

    Quote Originally Posted by Tadas View Post
    I need to calculate crc-16 for this string:
    0A0105000000102347455420444154414F524445520D0A01
    Then answer should be: F65B(hex)
    I can get this answer using online calculator http://www.zorc.breitbandkatze.de/crc.html
    just click button crc-16 and paste line below and click compute
    %0A%01%05%00%00%00%10%23%47%45%54%20%44%41%54%41%4 F%52%44%45%52%0D%0A%01
    this will give the correct answer F65B, but I want to integrate it in to my qt code.
    There are numerous sites with CRC code in C that you can use if the Qt CCITT variant is not to your liking.

    Regardless of which of the 16-bit CRC variant you use, it would certainly help if you fed the online CRC calculator and the Qt function the same input. Your code snippet feeds the Qt function a 48 byte string of characters, which is what your first sentence says you want to do. It seems obvious from your percent-encoded input to the online calculator that what you intended to do was send 24 bytes, the result of interpreting the original string as a hexadecimal representation of the bytes. Even using the same function this leads to different results:
    Qt Code:
    1. char *chars = "0A0105000000102347455420444154414F524445520D0A01";
    2. QByteArray bytes = QByteArray::fromHex(chars);
    3. quint16 crcChars = qChecksum(chars, strlen(chars));
    4. quint16 crcBytes = qChecksum(bytes.data(), bytes.length());
    5. qDebug() << strlen(chars) << "chars"
    6. << hex << crcChars << "hex"
    7. << dec << crcChars << "decimal";
    8. qDebug() << bytes.length() << "bytes"
    9. << hex << crcBytes << "hex"
    10. << dec << crcBytes << "decimal";
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. 48 chars 49fc hex 18940 decimal
    2. 24 bytes 55ae hex 21934 decimal
    To copy to clipboard, switch view to plain text mode 

    What I find interesting is that Qt's answers doesn't seem to match other implementations of the same CCITT CRC-16: 0x52A6 (string) and 0xD2C2 (binary).

  2. The following user says thank you to ChrisW67 for this useful post:

    Tadas (3rd June 2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.