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 10.2k 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.
  • B Bonnie

    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));
    
    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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

      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));
      
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011
      wrote on last edited by
      #5

      @Bonnie

      Thanks for Reply

      But it is not working

      JonBJ 1 Reply Last reply
      0
      • Ketan__Patel__0011K Ketan__Patel__0011

        @Bonnie

        Thanks for Reply

        But it is not working

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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?

        Ketan__Patel__0011K 1 Reply Last reply
        1
        • JonBJ JonB

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

          Ketan__Patel__0011K Offline
          Ketan__Patel__0011K Offline
          Ketan__Patel__0011
          wrote on 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

          JonBJ 1 Reply Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            @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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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.

            Ketan__Patel__0011K 1 Reply Last reply
            1
            • JonBJ JonB

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

              Ketan__Patel__0011K Offline
              Ketan__Patel__0011K Offline
              Ketan__Patel__0011
              wrote on 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
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                Ketan__Patel__0011K 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

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

                  Ketan__Patel__0011K Offline
                  Ketan__Patel__0011K Offline
                  Ketan__Patel__0011
                  wrote on 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 JonBJ 2 Replies Last reply
                  0
                  • Ketan__Patel__0011K Ketan__Patel__0011

                    @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 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
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      @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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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?

                      Ketan__Patel__0011K 1 Reply Last reply
                      0
                      • JonBJ JonB

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

                        Ketan__Patel__0011K Offline
                        Ketan__Patel__0011K Offline
                        Ketan__Patel__0011
                        wrote on 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

                        JonBJ B 2 Replies Last reply
                        0
                        • Ketan__Patel__0011K Ketan__Patel__0011

                          @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

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 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?

                          Ketan__Patel__0011K 1 Reply Last reply
                          1
                          • Ketan__Patel__0011K Ketan__Patel__0011

                            @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 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
                            • JonBJ JonB

                              @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__0011K Offline
                              Ketan__Patel__0011K Offline
                              Ketan__Patel__0011
                              wrote on 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

                              JonBJ mrjjM 2 Replies Last reply
                              0
                              • Ketan__Patel__0011K Ketan__Patel__0011

                                @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

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 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
                                • Ketan__Patel__0011K Ketan__Patel__0011

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

                                  • Login

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