Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. std::string to QString can't convert properly
Forum Updated to NodeBB v4.3 + New Features

std::string to QString can't convert properly

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 9.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 23 May 2020, 07:16 last edited by
    #2

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

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    2
    • B Offline
      B Offline
      Bonnie
      wrote on 23 May 2020, 07:18 last edited by
      #3

      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));
      
      C K 2 Replies Last reply 23 May 2020, 07:35
      1
      • B Bonnie
        23 May 2020, 07:18

        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));
        
        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 23 May 2020, 07:35 last edited by
        #4

        @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)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • B Bonnie
          23 May 2020, 07:18

          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));
          
          K Offline
          K Offline
          Ketan__Patel__0011
          wrote on 23 May 2020, 07:50 last edited by
          #5

          @Bonnie

          Thanks for Reply

          But it is not working

          J 1 Reply Last reply 23 May 2020, 08:30
          0
          • K Ketan__Patel__0011
            23 May 2020, 07:50

            @Bonnie

            Thanks for Reply

            But it is not working

            J Offline
            J Offline
            JonB
            wrote on 23 May 2020, 08:30 last edited by
            #6

            @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?

            K 1 Reply Last reply 23 May 2020, 09:03
            1
            • J JonB
              23 May 2020, 08:30

              @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?

              K Offline
              K Offline
              Ketan__Patel__0011
              wrote on 23 May 2020, 09:03 last edited by
              #7

              @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

              J 1 Reply Last reply 23 May 2020, 09:30
              0
              • K Ketan__Patel__0011
                23 May 2020, 09:03

                @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

                J Offline
                J Offline
                JonB
                wrote on 23 May 2020, 09:30 last edited by JonB
                #8

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

                K 1 Reply Last reply 23 May 2020, 09:45
                1
                • J JonB
                  23 May 2020, 09:30

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

                  K Offline
                  K Offline
                  Ketan__Patel__0011
                  wrote on 23 May 2020, 09:45 last edited by
                  #9

                  @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³";
                  
                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 23 May 2020, 09:46 last edited by
                    #10

                    @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?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    K 1 Reply Last reply 23 May 2020, 09:56
                    1
                    • C Christian Ehrlicher
                      23 May 2020, 09:46

                      @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?

                      K Offline
                      K Offline
                      Ketan__Patel__0011
                      wrote on 23 May 2020, 09:56 last edited by
                      #11

                      @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

                      B J 2 Replies Last reply 23 May 2020, 10:14
                      0
                      • K Ketan__Patel__0011
                        23 May 2020, 09:56

                        @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

                        B Offline
                        B Offline
                        Bonnie
                        wrote on 23 May 2020, 10:14 last edited by Bonnie
                        #12

                        @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!

                        1 Reply Last reply
                        1
                        • K Ketan__Patel__0011
                          23 May 2020, 09:56

                          @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

                          J Offline
                          J Offline
                          JonB
                          wrote on 23 May 2020, 10:25 last edited by
                          #13

                          @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?

                          K 1 Reply Last reply 23 May 2020, 11:59
                          0
                          • J JonB
                            23 May 2020, 10:25

                            @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?

                            K Offline
                            K Offline
                            Ketan__Patel__0011
                            wrote on 23 May 2020, 11:59 last edited by
                            #14

                            @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

                            J B 2 Replies Last reply 23 May 2020, 12:05
                            0
                            • K Ketan__Patel__0011
                              23 May 2020, 11:59

                              @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

                              J Offline
                              J Offline
                              JonB
                              wrote on 23 May 2020, 12:05 last edited by
                              #15

                              @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?

                              K 1 Reply Last reply 23 May 2020, 12:19
                              1
                              • K Ketan__Patel__0011
                                23 May 2020, 11:59

                                @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

                                B Offline
                                B Offline
                                Bonnie
                                wrote on 23 May 2020, 12:09 last edited by Bonnie
                                #16

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

                                1 Reply Last reply
                                3
                                • J JonB
                                  23 May 2020, 12:05

                                  @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?

                                  K Offline
                                  K Offline
                                  Ketan__Patel__0011
                                  wrote on 23 May 2020, 12:19 last edited by Ketan__Patel__0011
                                  #17

                                  @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

                                  J mrjjM 2 Replies Last reply 23 May 2020, 12:23
                                  0
                                  • K Ketan__Patel__0011
                                    23 May 2020, 12:19

                                    @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

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 23 May 2020, 12:23 last edited by
                                    #18

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

                                    1 Reply Last reply
                                    1
                                    • K Ketan__Patel__0011
                                      23 May 2020, 12:19

                                      @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

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 23 May 2020, 12:26 last edited by mrjj
                                      #19

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

                                      1 Reply Last reply
                                      3

                                      11/19

                                      23 May 2020, 09:56

                                      • Login

                                      • Login or register to search.
                                      11 out of 19
                                      • First post
                                        11/19
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved