Results 1 to 4 of 4

Thread: Comparing QString and char*

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Comparing QString and char*

    I am trying to compare QString against char*, but I fail to do that:
    Qt Code:
    1. void Register::checkKey()
    2. {
    3. QFile kFile("license.key");
    4. if (!kFile.open(QIODevice::ReadOnly | QIODevice::Text))
    5. {
    6. sd.setText("The registration key doesn't exist.\n");
    7. sd.exec();
    8. exit(0);
    9. }
    10.  
    11. QTextStream key(&kFile);
    12. QString test = key.readLine();
    13. kFile.close();
    14. QString hash = GetMacAddress();
    15. char ser[22];
    16. hdidnt(ser);
    17. QByteArray Id(ser);
    18. hash += QString::fromUtf8(Id);
    19. char *crypt = hash.toUtf8().data();
    20. encrypt(crypt, "key");
    21. MD5 md;
    22. if (test != QString::fromUtf8(md.digestString(md.digestString(md.digestString(crypt)))))
    23. {
    24. sdf.setText(QString::fromUtf8(md.digestString(md.digestString(md.digestString(crypt)))) + "\n" \
    25. + test + "\n" \
    26. + QString::number(sizeof(test.toUtf8())) + "\n" \
    27. );
    28. sdf.exec();
    29. sd.setText("This product must have a valid key.\n");
    30. sd.exec();
    31. KeyDialog kDialog;
    32. kDialog.setKey(QString::fromUtf8(md.digestString(crypt)));
    33. kDialog.exec();
    34. exit(0);
    35. }
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 
    I tryied also to convert QString to char* then make the comparison but I fail too. Both QString test and md.digestString(md.digestString(md.digestString(cr ypt))) return some value but they unenable to compare.

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Comparing QString and char*

    I would write something like this ...
    Qt Code:
    1. QByteArray cryptByteArray = hash.toUtf8();
    2. char *crypt = cryptByteArray.data();
    To copy to clipboard, switch view to plain text mode 
    If you write directly
    Qt Code:
    1. char *crypt = hash.toUtf8().data();
    To copy to clipboard, switch view to plain text mode 
    then temporary QByteArray will no longer exists and "crypt" will point to dangled pointer.

  3. The following user says thank you to cincirin for this useful post:

    SIFE (4th January 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Comparing QString and char*

    Be sure to discard the newline character in case there is one in the file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Comparing QString and char*

    Thenks Mr cincirin, it works now.

Similar Threads

  1. Comparing a Qstring with a value defined in an enum
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 24th March 2011, 16:11
  2. Comparing QString causes the program to crash .
    By ladiesfinger in forum Qt Programming
    Replies: 9
    Last Post: 6th November 2010, 17:17
  3. QString to char*
    By giginjose in forum Newbie
    Replies: 5
    Last Post: 11th June 2010, 10:19
  4. Comparing the |(shift+\) char
    By warry in forum Newbie
    Replies: 1
    Last Post: 8th September 2008, 07:05
  5. QString to char
    By boss_bhat in forum Qt Programming
    Replies: 7
    Last Post: 20th July 2006, 23:26

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.