PDA

View Full Version : problem in data type conversion



sksingh73
19th June 2010, 19:14
i have made my own hash chain generator of a string in c++ in linux. now i am trying to make its gui in qt4. i have converted my c++ code to suit qt4 syntax. my hash function works as follows.
- user ask to enter a string.
- based on the string entered it calculates hash value, 10 ten times by putting it in a 'for' loop, so as to make a hashchain.
my problem is that it works fine in c++, but when i am converting to qt4 code, its only giving first value in chain as correct. but after that all values being generated are not correct i.e they are not same as that in c++. i think problem is in data conversion. below is my code. please help in correcting this code. thanx
in advance.

quint32 c;
QString phrase;
phrase = stringlineEdit->text();
char *str = phrase.toAscii().data(); // convert from qstring to *char
for(int i=0;i<10;i++)
{
c = hash(str, strlen(str), 0);
QString str1 = QVariant( c ).toString();
str1 = QString::number( c, 16 );
resulttextEdit->append( "Key: " + str1 );
str = str1.toAscii().data(); // convert from qstring to *char

}

ocratato
21st June 2010, 03:54
It is difficult to answer why you are getting different answers between Qt and std C++ without seeing what hash() does or understanding what you mean by a hashchain.

However, I would replace all the QString and QVariant stuff with a simple sprintf( buf, "Key: %d", c);

You might be bumping into some issues with locales, unicode and similar, so for your application you want to get back to basic ascii.

ChrisW67
24th June 2010, 02:12
char *str = phrase.toAscii().data(); // convert from qstring to *char
for(int i=0;i<10;i++)
{
c = hash(str, strlen(str), 0);
QString str1 = QVariant( c ).toString();
str1 = QString::number( c, 16 );
resulttextEdit->append( "Key: " + str1 );
str = str1.toAscii().data(); // convert from qstring to *char

}
Storing, and later using (read or write), a pointer to a temporary object (QByteArray data block created by toAscii()) is dangerous and possibly the cause of your issues.