Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

std::string to QString can't convert properly



  • Hello Experts

    i am getting problem with std::string and QString

    My Data Like This :

          std::string  Data = "Z¾xÞ?î?é?ðuفçzá?æ?ö?èæYÆV½MÃT¾T¿M³";
    

    Now i want to convert this string to QString But did't get the proper result
    i am tried following ways to covert std:::string to QString

        qDebug() << QString::fromStdString (Data) << endl;         
        qDebug() << QString::fromStdString (Data.c_str()) << endl;                  
        qDebug() << QString::fromUtf8 (Data.c_str()) << endl;                   
        qDebug() << QString::fromLocal8Bit (Data.c_str()) << endl;                  
        qDebug() << QString(Data.c_str()).toLocal8Bit().constData() << endl; 
    

    i did't get complete conversion using this ways

    if you know any other way to convert string properly then please Help me....

    Please help me to solve this problem


  • Qt Champions 2019

    @Ketan__Patel__0011 said in std::string to QString can't convert properly:

    Please help me to solve this problem

    You have to know which encoding the std::string is and use the correct conversion with the help of e.g. QTextCodec.



  • I suppose your cpp file is saved in UTF8, then the first one is the right way to go.
    But some characters cannot be printed by QDebug (I don't know why).
    You can show the text in a text edit or something else, and you'll see the result is right.

        ui->textEdit->setPlainText(QString::fromStdString (Data));
    

  • Qt Champions 2019

    @Bonnie said in std::string to QString can't convert properly:

    But some characters cannot be printed by QDebug (I don't know why).

    They can, when the console supports utf-8 (windows cmd.exe can not)



  • @Bonnie

    Thanks for Reply

    But it is not working



  • @Ketan__Patel__0011 said in std::string to QString can't convert properly:

    But it is not working

    What is not working? Did you read and act on each of @Bonnie's & @Christian-Ehrlicher's observations about Windows/codecs/qDebug()/QTextEdit? Do you want to tell people what is not working, or do you think it's better if they guess?



  • @JonB

    Yes i was read about windows / codecs / qDebug() / QTextEdit
    And you misunderstood about my reply

    and If i used this way

             qDebug() << QString::fromLocal8Bit (Data.c_str()) << endl;  
    

    Then i get most of data but i did't get all character same as std::string



  • @Ketan__Patel__0011
    But @Bonnie already told you:

    But some characters cannot be printed by QDebug (I don't know why).

    (which is true), and he said:

    You can show the text in a text edit or something else, and you'll see the result is right.

    ui->textEdit->setPlainText(QString::fromStdString (Data));
    

    So why try qDebug() output again, when he has made this suggestion for you to at least give a go?

    but i did't get all character same as std::string

    If that's really the case once you have tried QTextEdit, then check what the length is of the std::string vs that of the converted QString.



  • @JonB

    i am use this line in my code

                 ui->textEdit->setPlainText(QString::fromStdString(Data));
    

    and you can see output

    12.PNG

    and my actual string data string is :

         std::string  Data = "Z¾xÞ?î?é?ðuفçzá?æ?ö?èæYÆV½MÃT¾T¿M³";

  • Qt Champions 2019

    @Ketan__Patel__0011 said in std::string to QString can't convert properly:

    and my actual string data string is :

    again: which encoding is this string?



  • @Christian-Ehrlicher

    i generate this string using my encryption algorithm and i am using some high logic for it

    Like Multiple Looping Statements And Leader Conditions



  • @Ketan__Patel__0011 I think maybe your cpp file is not saved in UTF8...
    But never mind, since the string is just an output of encryption algorithm...There'll be characters that even UTF8 cannot handle.
    You should know that we usually don't save that kind of string (or we should call it raw data) directly, because it is not readable / printable.
    We encode the raw data to hex / base64 string, and use that to save / transfer to others.
    And when we need the raw data, we decode from that hex / base64 string.
    If you don't want to save / transfer / make it readable, just want to get rid of a std::string and use Qt's class to store it, then you should use QByteArray instead of QString.
    So there won't be any option about "what codec should be used to convert the string" since it is not a really string!



  • @Ketan__Patel__0011
    Additional to @Bonnie's latest.

    i generate this string using my encryption algorithm

    So should it not be a byte array/vector rather than a string?



  • @JonB

    Thanks For Reply

    ByteArray Or Vector Is good concept but it's is not good for sensitive data

    and even it is not good for any encrypted data or any sensitive data

    And Otherthing is am able to write this data in any text file

    but when i try to save this string in my MYSQL Database that time it is can't store
    with complete and proper data some characters are changes

    Please Help me to solve this problem

    This problem spoiled my whole day



  • @Ketan__Patel__0011 said in std::string to QString can't convert properly:

    ByteArray Or Vector Is good concept but it's is not good for sensitive data
    and even it is not good for any encrypted data or any sensitive data

    No idea what you are talking about here. I never said you should not encrypt. I asked whether the result of the encryption isn't a byte array rather than a string as you say it is. It looks like bytes and not characters to me. But if you say it's text that's up to you.

    And Otherthing is am able to write this data in any text file

    Regardless of encoding/codec?

    but when i try to save this string in my MYSQL Database that time it is can't store
    with complete and proper data some characters are changes

    I wonder if that's because it's bytes and not text?



  • @Ketan__Patel__0011 I don't think anyone can / want to help you if you don't listen to anyone.
    Here I'll tell you what I usually do to store encrypted data to database.
    Option 1: I use QByteArray to store the encrypted data, and then save it to database as BLOB.
    Option 2: I convert it to hex / base64 string, and then save it to database as string.
    That's over. I'll not reply this post anymore.



  • @JonB

    my Whole Process

    1 -> define a normal string like:
    std::string data="Welcome For Process";
    2 -> Applying my encryption algorithm on this string
    3 -> then store encrypted string into my file
    4 -> Now for Backup i am try to save my encrypted string in MYSQL Database

    But i can't store my encrypted string properly, some character are changed

    otherthing is when open my encrypted string file in notepad
    then it show it is ANSI encoding

    Please help me to solve this problem

    Again My Major problem is
    i want to store my encrypted string in MYSQL Database



  • @Ketan__Patel__0011 said in std::string to QString can't convert properly:

    2 -> Applying my encryption algorithm on this string

    And how does your encryption algorithm work? Is it really producing text characters from text characters, or is it actually manipulating and producing bytes?

    otherthis is when open my encrypted string file in notepad
    then it show it is ANSI encoding

    Not sure that tells you anything from Notepad, but I'm not certain.

    Please help me to solve this problem

    So you keep saying.

    i want to store my encrypted string in MYSQL Database

    Yes, we understand this.

    @Bonnie's two options are indeed the normal way to save any encrypted data to a database.


  • Lifetime Qt Champion

    @Ketan__Patel__0011
    Hi
    Since its not unicode or UTF8 or real text at all you cannot just store it in the DB as it expects it to be valid text
    with valid encoding.

    So do as @Bonnie says and store them in a blop via QByteArray.
    Like this sample storing images.
    https://wiki.qt.io/How_to_Store_and_Retrieve_Image_on_SQLite

    Note, the type used for creating the table
    query.exec( "CREATE TABLE IF NOT EXISTS imgTable ( filename TEXT, imagedata BLOB )" );

    Then it won't alter your data and it should work.
    Your output from the encryption is no longer valid text. Treat it as a binary thing and
    use QByteArray in case it might add a zero in there. std::string and QString is not ok for this sort of data
    in many cases.


Log in to reply