Results 1 to 3 of 3

Thread: CRC-16 calculaion with qChecksum

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default CRC-16 calculaion with qChecksum

    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.

    I was hoping to calculate CRC using qChecksum function.
    Qt Code:
    1. QByteArray source = QString("0A0105000000102347455420444154414F524445520D0A01").toUtf8();
    2. quint16 crc1 = qChecksum(source.data(), source.length());
    3. qDebug() <<"crc1" <<crc1;
    To copy to clipboard, switch view to plain text mode 
    but answer is crc1 18940 (dec), 49FC (hex) , so it is wrong

    This page also has a link to C code, but it is hard for me to understand it, i'm new with bytes and bits. http://www.zorc.breitbandkatze.de/crctester.c
    Maybe someone could show me, how to do this calculation with qChecksum (if it is possible) or give any other hints.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: CRC-16 calculaion with qChecksum

    Read up on CRC algorithms; there are 13 conflicting definitions of CRC-16, according to the article. You will first have to determine which is used by Qt, then find an independent source for comparison that uses the same variant.

    Note that Qt seems to use CRC-16-CCITT.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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).

  4. 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.