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. QString convert accent error
QtWS25 Last Chance

QString convert accent error

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.5k Views
  • 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.
  • O Offline
    O Offline
    OPit
    wrote on last edited by
    #1

    Hi everybody,

    I have a problem with a conversion of my string when I send a request to a server. I'd like to send a file with the name "test_arrière.mp4", but Qt converts the character "è" with an hexa value, so the server send me an error :

    Malformed input or input contains unmappable characters: test_arri\xC3\xA8re
    

    How can I disable this conversion ? I tried different method of QString (fromUtf8, toLatin1, etc...) but I don't understand how to do it.

    NB : The HTTP header part which have the problem is QNetworkRequest::ContentDispositionHeader -> QVariant("form-data; name="file"; filename="test_arrière.mp4")

    Thanks

    KroMignonK 1 Reply Last reply
    0
    • O OPit

      Hi everybody,

      I have a problem with a conversion of my string when I send a request to a server. I'd like to send a file with the name "test_arrière.mp4", but Qt converts the character "è" with an hexa value, so the server send me an error :

      Malformed input or input contains unmappable characters: test_arri\xC3\xA8re
      

      How can I disable this conversion ? I tried different method of QString (fromUtf8, toLatin1, etc...) but I don't understand how to do it.

      NB : The HTTP header part which have the problem is QNetworkRequest::ContentDispositionHeader -> QVariant("form-data; name="file"; filename="test_arrière.mp4")

      Thanks

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @OPit Hello, accents and code pages are always something complex... If you C++ file is encoded as UTF8 (which I recommend) using QString::fromUtf8() should work:

      filename=QString::fromUtf8("test_arrière.mp4");
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      5
      • O Offline
        O Offline
        OPit
        wrote on last edited by
        #3

        Thanks for reply. My C++ file is well encoded to utf8.
        My string is a variable and come from QDir::entryList(..). fromUtf8() need a QBytearray, so how I convert my variable : toUtf8, toLatin1, ... ?

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

          @OPit said in QString convert accent error:

          so how I convert my variable : toUtf8, toLatin1, ... ?

          It depends on what you want - if you need utf8() I would guess .toLatin1() is not what you want, or?

          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
          • O OPit

            Thanks for reply. My C++ file is well encoded to utf8.
            My string is a variable and come from QDir::entryList(..). fromUtf8() need a QBytearray, so how I convert my variable : toUtf8, toLatin1, ... ?

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

            Hi @OPit: most often toUtf8() is the best choice, but it depends on how the other side wants it.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            1
            • O Offline
              O Offline
              OPit
              wrote on last edited by
              #6

              filename=QString::fromUtf8(localFile.toUtf8) or filename=QString::fromUtf8(localFile.toLatin1()) (with localFile = "test_arrière.mp4") give me the same error from server :

              response =  "{\n  \"timestamp\" : \"03-02-2020 09:35:32\",\n  \"status\" : 500,\n  \"error\" : \"Internal Server Error\",\n  \"exception\" : \"java.nio.file.InvalidPathException\",\n  \"message\" : \"Malformed input or input contains unmappable characters: test_arri\xC3\xA8re.mp4\",\n  \"path\" : \"/api/file/manual\"\n}"
              

              Here some examples of qDebug() outputs of my variable, why I can't see the good format output ??? :

              direct print  "test_arrière.mp4"
              toUtf8 print  "test_arri\xC3\xA8re.mp4"
              toLatin1 print  "test_arri\xE8re.mp4"
              fromUtf8(file.toUtf8()) print  "test_arrière.mp4"
              fromUtf8(file.toLatin1()) print  "test_arri�re.mp4"
              
              KroMignonK aha_1980A 2 Replies Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                So what exactly are you trying to achieve - where (and in which format) do you send your request to, what encoding does it need, how / from where to you read the filename?

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

                O 1 Reply Last reply
                0
                • O OPit

                  filename=QString::fromUtf8(localFile.toUtf8) or filename=QString::fromUtf8(localFile.toLatin1()) (with localFile = "test_arrière.mp4") give me the same error from server :

                  response =  "{\n  \"timestamp\" : \"03-02-2020 09:35:32\",\n  \"status\" : 500,\n  \"error\" : \"Internal Server Error\",\n  \"exception\" : \"java.nio.file.InvalidPathException\",\n  \"message\" : \"Malformed input or input contains unmappable characters: test_arri\xC3\xA8re.mp4\",\n  \"path\" : \"/api/file/manual\"\n}"
                  

                  Here some examples of qDebug() outputs of my variable, why I can't see the good format output ??? :

                  direct print  "test_arrière.mp4"
                  toUtf8 print  "test_arri\xC3\xA8re.mp4"
                  toLatin1 print  "test_arri\xE8re.mp4"
                  fromUtf8(file.toUtf8()) print  "test_arrière.mp4"
                  fromUtf8(file.toLatin1()) print  "test_arri�re.mp4"
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @OPit said in QString convert accent error:

                  Here some examples of qDebug() outputs of my variable, why I can't see the good format output ??? :

                  Can you try?

                  qDebug() << qUtf8Printable(localFile);
                  

                  And actually, what do you want to achieve? Have you trouble with JSon request building?

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    So what exactly are you trying to achieve - where (and in which format) do you send your request to, what encoding does it need, how / from where to you read the filename?

                    O Offline
                    O Offline
                    OPit
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    I read a list of local files and I want to send them to a distant server that I don't really know what formats it can read.

                    local file -> QDir::entryList () -> send to server via QNetworkAccessManager->post and QHttpMultiPart/QHttpPart objects -> server

                    I've shown you the result of qDebug() because I don't know if the "mistake" come from entryList or after. Why I can't see the character 'è' when I just make a qDebug() << file ?

                    @KroMignon :
                    the result is the same : qUtf8Printable(localFile) => test_arrière.mp4

                    I just want to send file to a server :(, but it appears that the server doesn't accept my filename in
                    ContentDispositionHeader field because of utf8 encoded character ...

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

                      @OPit said in QString convert accent error:

                      ContentDispositionHeader field because of utf8 encoded character ...

                      So as I already said - you have to find out what encoding the server accepts. If it is not UTF-8 you can't send this filename.

                      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
                      • O OPit

                        filename=QString::fromUtf8(localFile.toUtf8) or filename=QString::fromUtf8(localFile.toLatin1()) (with localFile = "test_arrière.mp4") give me the same error from server :

                        response =  "{\n  \"timestamp\" : \"03-02-2020 09:35:32\",\n  \"status\" : 500,\n  \"error\" : \"Internal Server Error\",\n  \"exception\" : \"java.nio.file.InvalidPathException\",\n  \"message\" : \"Malformed input or input contains unmappable characters: test_arri\xC3\xA8re.mp4\",\n  \"path\" : \"/api/file/manual\"\n}"
                        

                        Here some examples of qDebug() outputs of my variable, why I can't see the good format output ??? :

                        direct print  "test_arrière.mp4"
                        toUtf8 print  "test_arri\xC3\xA8re.mp4"
                        toLatin1 print  "test_arri\xE8re.mp4"
                        fromUtf8(file.toUtf8()) print  "test_arrière.mp4"
                        fromUtf8(file.toLatin1()) print  "test_arri�re.mp4"
                        
                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @OPit

                        filename=QString::fromUtf8(localFile.toUtf8)

                        that is nonsense.

                        filename=QString::fromUtf8(localFile.toLatin1())

                        that is even more nonsense.

                        First you have to check if the QString itself is correct, e.g. by printing it to a label:

                        ui->label.setText(localFile);
                        

                        Is that giving correct output?

                        Regards

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1
                        • O OPit

                          @Christian-Ehrlicher
                          I read a list of local files and I want to send them to a distant server that I don't really know what formats it can read.

                          local file -> QDir::entryList () -> send to server via QNetworkAccessManager->post and QHttpMultiPart/QHttpPart objects -> server

                          I've shown you the result of qDebug() because I don't know if the "mistake" come from entryList or after. Why I can't see the character 'è' when I just make a qDebug() << file ?

                          @KroMignon :
                          the result is the same : qUtf8Printable(localFile) => test_arrière.mp4

                          I just want to send file to a server :(, but it appears that the server doesn't accept my filename in
                          ContentDispositionHeader field because of utf8 encoded character ...

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #12

                          @OPit said in QString convert accent error:

                          ContentDispositionHeader field because of utf8 encoded character

                          Yes, this is clearly your problem... What is you server await as string encoding type? Qt will always work in UTF8 internally, but you can transform this UTF8 string in to whatever you need.

                          For example, in your Terminal, you seed "test_arrière.mp4", because you are showing an UTF8 string in you terminal which, I supposed is windows terminal. This should work:

                          QTextCodec *codec = QTextCodec::codecForName("IBM850");
                          qDebug() << codec->fromUnicode(localFile);
                          

                          So you have to known which kind of text encoding you server is using.

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          3
                          • O Offline
                            O Offline
                            OPit
                            wrote on last edited by
                            #13

                            Thanks a lot, I understand.

                            The last thing. It's very strange. When I run the code below on my main file (encoded to utf8) :

                            QStringList files = QDir("D:/temp").entryList(QStringList() << "*.mp4");
                            QTextCodec * codec = QTextCodec::codecForLocale();
                            qDebug() << codec->toUnicode(file[0].toLatin1());
                            
                            --> test_arrière.mp4
                            

                            I have the good result.

                            But when I run the same code in my transfer class (also encoded to utf8), the result is wrong ???

                            --> test_arrière.mp4
                            

                            Why ?

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

                              @OPit said in QString convert accent error:

                              But when I run the same code in my transfer class (also encoded to utf8), the result is wrong ???

                              Again: how do you add it to the http request, does your server understand the encoding you send?
                              What you're doing with qDebug() works by accident and is completely nonsense.

                              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

                              • Login

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