PDA

View Full Version : Comparing QString and char*



SIFE
29th December 2011, 23:10
I am trying to compare QString against char*, but I fail to do that:

void Register::checkKey()
{
QMessageBox sd;
QFile kFile("license.key");
if (!kFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
sd.setText("The registration key doesn't exist.\n");
sd.exec();
exit(0);
}

QTextStream key(&kFile);
QString test = key.readLine();
kFile.close();
QString hash = GetMacAddress();
char ser[22];
hdidnt(ser);
QByteArray Id(ser);
hash += QString::fromUtf8(Id);
char *crypt = hash.toUtf8().data();
encrypt(crypt, "key");
MD5 md;
if (test != QString::fromUtf8(md.digestString(md.digestString( md.digestString(crypt)))))
{
QMessageBox sdf;
sdf.setText(QString::fromUtf8(md.digestString(md.d igestString(md.digestString(crypt)))) + "\n" \
+ test + "\n" \
+ QString::number(sizeof(test.toUtf8())) + "\n" \
);
sdf.exec();
sd.setText("This product must have a valid key.\n");
sd.exec();
KeyDialog kDialog;
kDialog.setKey(QString::fromUtf8(md.digestString(c rypt)));
kDialog.exec();
exit(0);
}

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

cincirin
31st December 2011, 08:53
I would write something like this ...


QByteArray cryptByteArray = hash.toUtf8();
char *crypt = cryptByteArray.data();

If you write directly


char *crypt = hash.toUtf8().data();

then temporary QByteArray will no longer exists and "crypt" will point to dangled pointer.

wysota
1st January 2012, 21:30
Be sure to discard the newline character in case there is one in the file.

SIFE
4th January 2012, 21:19
Thenks Mr cincirin, it works now.