Results 1 to 3 of 3

Thread: problem in data type conversion

  1. #1
    Join Date
    Jun 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default problem in data type conversion

    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

    }

  2. #2
    Join Date
    Apr 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem in data type conversion

    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.

  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: problem in data type conversion

    Quote Originally Posted by sksingh73 View Post
    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.

Similar Threads

  1. QtSql data type mappings
    By eclarkso in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2009, 17:22
  2. Using QVariant with custom data type
    By QAlex in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2009, 12:04
  3. Data type parameters.
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 09:00
  4. Data type error
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2008, 14:01
  5. conversion of binay data to qt types
    By rajeshs in forum Qt Programming
    Replies: 16
    Last Post: 4th January 2008, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.