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. Character encoding problem
Qt 6.11 is out! See what's new in the release blog

Character encoding problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 2.3k 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.
  • KutyusK Offline
    KutyusK Offline
    Kutyus
    wrote on last edited by
    #1

    Hi!

    I have some hungarian text stored in mysql db. The insertion is done from a windows program, it works with wchar_t strings. Before insert a string, I convert the data with WideCharToMultibyte() function, because the database's character set is UTF8, after select, I have back the original string with MultiByteToWideChar() function.
    How can I convert a string stored in the database to display in a QLabel, for example the hungarian 'é' letter seems to be 'é', this is two utf8 character from this raw data: \xC3\x83\xC2\xA1.

    Thanks in advance.

    aha_1980A 1 Reply Last reply
    0
    • KutyusK Kutyus

      Hi!

      I have some hungarian text stored in mysql db. The insertion is done from a windows program, it works with wchar_t strings. Before insert a string, I convert the data with WideCharToMultibyte() function, because the database's character set is UTF8, after select, I have back the original string with MultiByteToWideChar() function.
      How can I convert a string stored in the database to display in a QLabel, for example the hungarian 'é' letter seems to be 'é', this is two utf8 character from this raw data: \xC3\x83\xC2\xA1.

      Thanks in advance.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      @Kutyus With QString::fromUtf8() (or this overload)

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • KutyusK Offline
        KutyusK Offline
        Kutyus
        wrote on last edited by Kutyus
        #3

        This not working. The string type is wchar_t, only storing format is uft8. Now I see the utf8 result, this is wrong.

        aha_1980A 1 Reply Last reply
        0
        • KutyusK Kutyus

          This not working. The string type is wchar_t, only storing format is uft8. Now I see the utf8 result, this is wrong.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by aha_1980
          #4

          @Kutyus I don't understand. The result of WideCharToMultibyte() is LPSTR, i.e. 8 bit character, not wchar_t.

          You should probably show some code how you insert and retrieve the data from the DB to get better help.

          Qt has to stay free or it will die.

          1 Reply Last reply
          2
          • KutyusK Offline
            KutyusK Offline
            Kutyus
            wrote on last edited by
            #5

            I was wrong, the stored string is utf8, but in hungarian language, the 'é' is correct result in latin-1 codepage.
            My new problem is how to translate it?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Where do you use latin-1 now? Thought you're using a QString. Please show some code...

              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
              • KutyusK Offline
                KutyusK Offline
                Kutyus
                wrote on last edited by
                #7

                I was wrong again, it is a double encoding problem. The 'é' is 'é' in utf8, and 'é' is \xc3\x83\xc2\xa9 in utf8 and this last 4 byte is in my database.
                After select it the

                label.setText((query.value(7).toString());
                

                line is decoding one time, that's why i get the 'é'. So I would like to decode it one more time, but how? I tried more options without success.

                1 Reply Last reply
                0
                • KutyusK Offline
                  KutyusK Offline
                  Kutyus
                  wrote on last edited by
                  #8

                  I get the correct string with this query:

                  convert(cast(convert(col using latin1) as binary) using utf8)
                  

                  How can it be done in a qt?

                  1 Reply Last reply
                  0
                  • aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Kutyus

                    have you tried:

                    label.setText(QString::fromUtf8(query.value(7).toByteArray()));

                    ?

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    0
                    • KutyusK Offline
                      KutyusK Offline
                      Kutyus
                      wrote on last edited by Kutyus
                      #10

                      Yes, same wrong result.
                      And I tried these too:

                      label.setText(QString::fromUtf8(QByteArray::fromStdString(QString::fromUtf8(query.value(7).toByteArray()).toStdString())));
                      

                      and

                      label.setText(QString::fromUtf8(QByteArray::fromStdString(QString::fromLatin1(query.value(7).toByteArray()).toStdString())));
                      

                      but same wrong result.

                      1 Reply Last reply
                      0

                      • Login

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