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
Forum Updated to NodeBB v4.3 + New Features

Character encoding problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.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.
  • K Offline
    K Offline
    Kutyus
    wrote on 1 May 2019, 15:21 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.

    A 1 Reply Last reply 1 May 2019, 15:25
    0
    • K Kutyus
      1 May 2019, 15:21

      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.

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 1 May 2019, 15:25 last edited by aha_1980 5 Jan 2019, 15:26
      #2

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

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • K Offline
        K Offline
        Kutyus
        wrote on 1 May 2019, 15:27 last edited by Kutyus 5 Jan 2019, 15:28
        #3

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

        A 1 Reply Last reply 1 May 2019, 15:35
        0
        • K Kutyus
          1 May 2019, 15:27

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 1 May 2019, 15:35 last edited by aha_1980 5 Jan 2019, 15:35
          #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
          • K Offline
            K Offline
            Kutyus
            wrote on 1 May 2019, 17:25 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
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 1 May 2019, 18:03 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
              • K Offline
                K Offline
                Kutyus
                wrote on 4 May 2019, 07:48 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
                • K Offline
                  K Offline
                  Kutyus
                  wrote on 4 May 2019, 08:30 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
                  • A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 4 May 2019, 08:36 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
                    • K Offline
                      K Offline
                      Kutyus
                      wrote on 4 May 2019, 10:27 last edited by Kutyus 5 Apr 2019, 10:28
                      #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

                      1/10

                      1 May 2019, 15:21

                      • Login

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